Harikrishnan N
Harikrishnan N

Reputation: 874

Navigating to another XAML file

I have three XAML files, they are mainwindow.xaml,login.xaml,homepage.xaml. Since the files can be navigated through frames, i added a frame to main window which fits the whole screen.

XAML of MainWindow:

<Window x:Class="Myproject.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" WindowState="Maximized" Initialized="Window_Initialized">
    <Grid>
        <Frame Name="pageFrame"></Frame>
    </Grid>
</Window>

CS file of MainWindow:

private void Window_Initialized(object sender, EventArgs e)
{
   pageFrame.Height = SystemParameters.WorkArea.Height-10;
   pageFrame.Width = SystemParameters.WorkArea.Width;
   pageFrame.Navigate(new login());
}

It navigates perfectly to login page and perform login actions there.

The problem is it does not navigate to the homepage.xaml from the login.xaml.cs

Code used for navigating to homepage.xaml from login.xaml.cs

MainWindow mw = new MainWindow();
mw.pageFrame.Navigate(new homepage());

This code goes in a if statement section and i checked by using breakpoints if these statements are executed. And it does execute those and the objects are populated but the naviagtion does not occur.

What am i doing wrong? Is this not the correct approach?

Upvotes: 0

Views: 301

Answers (1)

kennyzx
kennyzx

Reputation: 13003

The problem is, well, mw is a new window and it is not even shown at all. And you are staying in your old instance of MainWindow, nothing happens to your old MainWindow.

You need to navigate from within your old MainWindow, not a new one.

((MainWindow)(Application.Current.MainWindow)).pageFrame.Navigate(new homepage());

You have a reference to your main window, Application.Current.MainWindow, but you need to cast it into your own type of MainWindow first.

Upvotes: 1

Related Questions