Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i navigate one xaml page to another?

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 ?

like image 303
loki Avatar asked Apr 25 '10 18:04

loki


People also ask

How to navigate from one page to another page in Xamarin?

You will click the Second Page button. Now, you will see the Second Page successfully. You will click the Main Page button. You will see the Main Page. This was the process of how to navigate from one page to another page in Xamarin.Forms, using Visual Studio.

How do I add a mainpage in XAML?

Step 6 In this step, add one Page, whose name is called MainPage.xaml. Go to Solution Explorer-->Your Project-->Portable-->Right click-->Add-->New Item (Ctrl+Shift+A). Step 7 Now, select Forms XAML page and give the name (MainPage.xaml).

How do I create a form XAML page in Visual Studio?

Go to Solution Explorer-->Your Project-->Portable-->Right click-->Add-->New Item (Ctrl+Shift+A). Step 7 Now, select Forms XAML page and give the name (MainPage.xaml). Step 8 In this step, add another one page, whose name is called SecondPage.xaml.

How to create gotobasicpage2 using XAML code?

And set the name through XAML code as Content="GoToMainPage" given here and the Navigation Page Detail here Page2->MainPage Navigation. 1 Now click on the "GoToBasicPage1" button. You will navigate to Page 1 as shown here: 2 Click on the "GoToBasicPage2" button. You will navigate to Page 2 as shown here:


2 Answers

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.

like image 143
Mostafa Al-Kaouri Avatar answered Sep 28 '22 02:09

Mostafa Al-Kaouri


Like AnthonyWJones said you need to use the navigation framework.

First you'll need to add a reference to System.Windows.Controls.Navigation in your project and refernce it in you MainPage.xaml

xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

Then you'll need a frame within where you'll switch different XAML pages. Something like this:

<navigation:Frame x:Name="navFrame" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Source=”/Views/First.xaml” />

Now somewhere in MainPage.xaml you could have a Button with a tag

<Button Click="Button_Click" Tag="/Views/Second.xaml" Content="Second" />

and in the Button_Click eventhandler you could switch out the content showed in navFrame.

private void Button_Click(object sender, RoutedEventArgs e)
{
    Button theButton = sender as Button;
    string url = theButton.Tag.ToString();

    this.navFrame.Navigate(new Uri(url, UriKind.Relative));
}

A cool thing to note is that by using NavigationFramework the browser back and forward buttons work perfectly and the URL in the addressbar updates depending on the XAML page you are currently on :)

like image 42
texmex5 Avatar answered Sep 28 '22 02:09

texmex5