How can i navigate one xaml page to another?

◇◆丶佛笑我妖孽 提交于 2019-12-04 02:59:12
texmex5

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 :)

Mostafa Al-Kaouri

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.

Looks like you've started off on the wrong foot. This sort of thing is catered for using the Navigation application template. You should start a new project and select "Silverlight Navigation Application".

Once loaded just run it to see what the basic shell looks like. Then take a look at how MainPage is structured and say the Home view. What you will need to do is create new views based on the navigation Page type and then add them to MainPage.xaml.

The simple one to solve this problem, you can look at this website : http://blogs.microsoft.co.il/blogs/eladkatz/archive/2011/01/25/adapting-silverlight-navigation-to-mvvm.aspx .

I had this problem to earlier. But after I read this tutorial, I can easily navigating to another view with MVVM. Hope this can help you all solve the problem.Thx

Shravankumar
private void formcombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach (ComboBoxItem child in formcombobox.Items)
    {
        if (child.Name != null && child.IsSelected == true)
        {

            string url = new System.Uri("/DWRWefForm;component/Pages/"
                            + child.Name + ".xaml", System.UriKind.Relative).ToString();
            this.navframe.Navigate(new Uri(url, UriKind.Relative)); 
        }

    }
}
MAyadi

Try this:

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

It's working for me. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!