如何将一个xaml页面导航到另一个? [英] How can i navigate one xaml page to another?

查看:269
本文介绍了如何将一个xaml页面导航到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2页面,我需要导航mainpage.xaml到login.page xaml,但它抛出我
对象引用未设置为对象的实例在Root.Children。

我在App.xaml中添加了这些代码:  private void Application_Startup(object sender,StartupEventArgs e)
{
Grid myGrid = new Grid();
myGrid.Children.Add(new MainPage());
this.RootVisual = myGrid;
}

,然后在main.xaml上添加一些代码以导航到LoginUI.xaml

 命名空间Gen.CallCenter.UI 
{
public partial class MainPage:UserControl
{
public MainPage()
{
InitializeComponent();

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

如何将main.xaml导航到LoginUI。 xaml?

解决方案

AnthonyWJones 表示您需要使用导航框架。



需要在您的项目中添加对 System.Windows.Controls.Navigation 的引用,并在您的MainPage.xaml中引用它

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

然后你需要一个框架,你可以在其中切换不同的XAML页面。像这样:

 < nav:Frame x:Name =navFrameHorizo​​ntalContentAlignment =StretchVerticalContentAlignment =Stretch Source =/ Views / First.xaml/> 

现在MainPage.xaml中的某个地方可以有一个带有标签的Button



< Button Click =Button_ClickTag =/ Views / Second.xamlContent =Second/>



并且在 Button_Click eventhandler中,您可以切换 navFrame

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

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

一个很酷的事情要注意,使用NavigationFramework浏览器的后退和前进按钮工作完美,地址栏中的网址会根据您当前所在的XAML页面进行更新:)


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

这篇关于如何将一个xaml页面导航到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆