1

I have a frame inside mainwindow, inside it there's a page with panels and various contents. The mainwindow decides wich page to load, and then must interact with their contents (here is the problem).

I've tried many solution and the best is this, but returns pageLogin as a null object

    _mainFrame.Source = new Uri(@"/Pages/Login.xaml", UriKind.Relative);
    Page pageLogin = this._mainFrame.Content as Page;

where _mainFrame is of course the name of the frame inside t mainwindow, and Login.xaml is the content with the Login_panel Stackpanel inside

Fehu
  • 381
  • 1
  • 3
  • 18
  • Does Login.Xaml inherit from Page? Or does it inherit from Window or UserControl? – Rhyous Feb 27 '14 at 21:37
  • Also, in your debugger in Visual Studio you should be able to see what object this._mainFrame.Content actual is. – Rhyous Feb 27 '14 at 21:38
  • The _mainFrame perfectly shows the page, but I need to interact with the contens – Fehu Feb 27 '14 at 21:43
  • Yes, it inherits from Window.Controls.Page – Fehu Feb 27 '14 at 21:44
  • With your code, pageLogin will only be null if this._mainFrame.Content is null or not a Page object. – Rhyous Feb 27 '14 at 22:00
  • I'll better show more code This is the Login.xaml class ` namespace Mosaic.Pages { public partial class Login : Page { public Login() { InitializeComponent(); } } } ` – Fehu Feb 27 '14 at 22:13
  • I am also seriously concerned with what you are trying to do. Have you read about MVVM? You really should never need to have to do what you are trying to do. See this link: http://www.wpfsharp.com/2011/04/05/navigation-and-pages-using-model-view-viewmodel-mvvm/ – Rhyous Feb 27 '14 at 23:00
  • Yes, but it's a simple project that I'm using also to better learn basics. I want to end to mvvm/prism, but all that I've read until now is a little too specific or too theoric for me to grasp at the moment – Fehu Feb 27 '14 at 23:04

1 Answers1

7

OK. So first, I think you might be going about this the wrong way to start. Check out this project. http://www.wpfsharp.com/2011/04/05/navigation-and-pages-using-model-view-viewmodel-mvvm/

Try this example:

MainWindow.xaml

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded"
        >
    <Grid>
        <Frame Name="MainFrame" Source="/Login.xaml"></Frame>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var a = MainFrame.Content as Page;
            var grid = a.Content as Grid;
            var textBlock = grid.Children[0];
            // bla bla, you logged in
            MainFrame.Source = new Uri("/Home.xaml", UriKind.Relative);
            var b = MainFrame.Content as Page; // Still Login.xaml
            MainFrame.ContentRendered +=MainFrame_ContentRendered;
        }

        private void MainFrame_ContentRendered(object sender, EventArgs e)
        {
            var b = MainFrame.Content as Page; // Is now Home.xaml
        }
    }
}

Login.xaml

<Page x:Class="Test.Login"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="Login">

    <Grid>
        <TextBlock>This is a sample login page.</TextBlock>
    </Grid>
</Page>
Rhyous
  • 6,510
  • 2
  • 44
  • 50
  • I think that's the solution, but Window_Loaded is not being fired... (I'm still a noob in c# :/) – Fehu Feb 27 '14 at 22:47
  • Did you add Loaded="Window_Loaded" to the MainWindow.xaml in the tag? – Rhyous Feb 27 '14 at 22:49
  • Thanks I knowed I was missing something important :S Now it is fired but throw an unmanaged System.NullReferenceException on var grid = a.Content as Grid; – Fehu Feb 27 '14 at 22:53
  • I've checked and is the a that is null, but the page content is loaded in the frame – Fehu Feb 27 '14 at 22:56
  • It is because you aren't giving it time to render. See my MainWindow.xaml in my updated answer. – Rhyous Feb 27 '14 at 22:59
  • I'm still a little confused so it's best to follow your advice and return to study some more – Fehu Feb 27 '14 at 23:10