gpt4 book ai didi

c# - 如何从 App.xaml.cs 导航到其他页面?

转载 作者:行者123 更新时间:2023-12-05 07:23:53 26 4
gpt4 key购买 nike

所以我的应用程序中有不同的页面。因为我想要跨所有这些页面的菜单栏,所以我在我的 App.xaml 中做了以下内容。

我通常会使用 NavigationService 在不同页面之间导航。但是如何从我的 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>

我在菜单项上获得了页面 StatussenPage.xaml 等,点击它应该显示该页面等。

将以下代码添加到我的 App.xaml.cs 中:

        Page testpage = new TipsTricksPage();

private void MenuItem_OnClick(object sender, RoutedEventArgs e)
{
testpage.NavigationService.Navigate(new TipsTricksPage());
}

出现以下错误:系统.NullReferenceException

最佳答案

在最佳实践中,您应该使用 MainWindow 启动您的应用

在 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">

然后,在您的主窗口(应用程序启动时直接打开)中,您可以创建一个菜单并在 MainWindow.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>

在 MainWindow.cs 中

        private void MyActionClick(object sender, RoutedEventArgs e)
{
//Your code
}

关于c# - 如何从 App.xaml.cs 导航到其他页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55683909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com