How can i navigate one xaml page to another?

loki picture loki · Apr 25, 2010 · Viewed 48.6k times · Source

i have 2 page i need to navigate mainpage.xaml to login.page xaml but it throws me Object reference not set to an instance of an object. in Root.Children.Clear();....

i added this codes in App.xaml:

   private void Application_Startup(object sender, StartupEventArgs e)
        {
            Grid myGrid = new Grid();
            myGrid.Children.Add(new MainPage());
            this.RootVisual = myGrid;
       }

and than i adde some codes on main.xaml to navigate to LoginUI.xaml

namespace Gen.CallCenter.UI
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            Grid Root = ((Grid)(this.Parent));
            Root.Children.Clear();
            Root.Children.Add(new LoginUI());
        }
    }
}

How can i navigate main.xaml to LoginUI.xaml ?

Answer

Mostafa Al-Kaouri picture Mostafa Al-Kaouri · Aug 16, 2012

Let's suppose you are viewing the MainPage.xaml then you want to open another xaml page called newPage.xaml by clicking on a Button or an ImageEdit in the MainPage.xaml, here's the quick solution that you should write inside the MainPage.xaml.cs:

private void imageEdit1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    newPage mynewPage = new newPage(); //newPage is the name of the newPage.xaml file
    this.Content = mynewPage;
}

This is working with me.