[英]How to navigate from App.xaml.cs to other pages?
So I have different pages in my application. 所以我的应用程序中有不同的页面。 Since I want a menubar across all of these pages I made the following in my App.xaml.
由于我想要所有这些页面上的菜单栏,因此在App.xaml中进行了以下操作。
I would normally use NavigationService to navigated between different pages. 我通常会使用NavigationService在不同页面之间导航。 But how do I navigate to different pages from my App.xaml.cs.
但是,如何从App.xaml.cs导航到其他页面。
<Application.Resources>
<Menu x:Key="Menu">
<DockPanel VerticalAlignment="Top">
<Menu DockPanel.Dock="Top" FontSize="14">
<MenuItem Header="_File">
<Separator />
<MenuItem Header="_Exit" />
</MenuItem>
<MenuItem Header="_Statussen" Click="MenuItem_OnClick"/>
<MenuItem Header="_TipsTricks" />
</Menu>
</DockPanel>
</Menu>
</Application.Resources>
I got pages StatussenPage.xaml etc. on menuitem click it should show that page etc. 我在menuitem上获得了StatusstatPage.xaml等页面,单击它应显示该页面等。
Added following code to my App.xaml.cs: 在我的App.xaml.cs中添加了以下代码:
Page testpage = new TipsTricksPage();
private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
testpage.NavigationService.Navigate(new TipsTricksPage());
}
And getting following error: System.NullReferenceException 并得到以下错误:System.NullReferenceException
In best practice, You should start your app with a MainWindow . 在最佳实践中, 您应该使用MainWindow启动应用程序 。
In App.xaml 在App.xaml中
<Application x:Class="projectname.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Banc_Suspension_SAHD"
StartupURI="MainWindow.xaml.cs" // Right here !
Exit="App_Exit">
then, IN your MainWindow (open directly when app start) you can create a Menu and in the MainWindow.cs treat click event. 然后,在您的MainWindow(应用启动时直接打开)中,您可以创建一个菜单,并在MainWindow.cs中处理单击事件。
In MainWindow.Xaml.cs 在MainWindow.Xaml.cs中
<Window x:Class="projectname.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Menu>
<MenuItem Header="Re-Impression PV">
<MenuItem Click = "MyActionClick"></MenuItem>
<MenuItem></MenuItem>
</MenuItem>
<Menu/>
</Grid>
</Window>
In MainWindow.cs 在MainWindow.cs中
private void MyActionClick(object sender, RoutedEventArgs e)
{
//Your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.