[英]How to navigate from mainwindow.xaml to page.xaml in WPF
我知道如何使用从一个“page.xaml”导航到另一个“page.xaml”
this.NavigationService.Navigate(new Uri("Pages/Page2.xaml", UriKind.Relative));
但我需要代码从主“window.xaml”导航到“page.xaml”。
您应该有一个Page
,该Page
显示在您的Frame
任何其他页面之前。 然后可以进行通常的导航。
这是应该保留在您的Window.xaml
XAML 示例:
<Grid Name="MainGrid">
<Frame Name="LeftNavigationFrame"
Grid.Column="0" >
</Frame>
</Grid>
在你的 .xaml.cs 中:
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
LeftNavigationFrame.NavigationService.Navigate(new Uri(...));
}
}
如果您愿意,这里是另一个选项,使用App.xaml.cs
,您的应用程序的主要入口点。 导航到第一页是在OnLaunched
方法中完成的,如果项目是使用 Visual Studio 新建项目向导创建的,那么您应该已经在那里使用了此方法。
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// Here you can navigate to some other pages if saved some sort of state
}
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// Replace MainPage with your desire page
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.