我有2个页面,我需要导航mainpage.xaml到login.page xaml,但是它使我失望
你调用的对象是空的。在Root.Children.Clear(); ....
我在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
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());
}
}
}
如何将main.xaml导航到LoginUI.xaml?
最佳答案
就像AnthonyWJones所说,您需要使用导航框架。
首先,您需要在项目中添加对System.Windows.Controls.Navigation
的引用,并在MainPage.xaml中对其进行引用
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
然后,您将需要一个框架,在其中切换不同的XAML页面。像这样的东西:
<navigation:Frame x:Name="navFrame" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Source=”/Views/First.xaml” />
现在在MainPage.xaml中的某个位置,您可以拥有一个带有标签的Button
<Button Click="Button_Click" Tag="/Views/Second.xaml" Content="Second" />
在
Button_Click
事件处理程序中,您可以切换出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));
}
需要注意的一件很酷的事情是,通过使用NavigationFramework,浏览器的后退和前进按钮可以正常工作,并且地址栏中的URL会根据您当前所在的XAML页面进行更新:)
关于silverlight - 如何将一个xaml页面导航到另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709274/