WinUI 3.0 桌面 - C# 页面导航
WinUI 3.0 Desktop - C# Page Navigation
我正在尝试进行简单的页面导航,但我找不到任何有关如何在 WinUI 3.0 中进行此操作的文档。
目前,当我使用 WinUI 3.0 创建空白应用程序时,我在 App.xaml.cs
中创建了以下代码
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
}
private Window m_window;
虽然在我在网上找到的许多其他示例中,根框架是在上面的 OnLaunched 事件中定义的。
我要如何定义MainWindow.xaml或App.xaml才能得到一个可以在Page1.xaml和Page2.xaml之间自由切换的框架?
编辑:
我现在发现我可以通过调用来检索框架:
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
Frame rootFrame = m_window.Content as Frame;
m_window.Activate();
rootFrame.Navigate(typeof(UI.MainMenu));
}
但是导航失败并出现 System.NullReferenceException: 'Object reference not set to an instance of an object.'
错误。我做错了什么 :S?
所以,我设法找到了一种方法。
首先,我创建了一个 window,我将其称为 NavigationRootWindow。
<Window
x:Class="Whosebug.UI.NavigationRootWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Whosebug.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Frame x:Name="rootFrame"/>
</Grid>
在这里,我添加了一个名为 rootFrame 的框架。
现在,在应用程序中,我将 onLaunched 函数定义为自动生成的,但我确保添加的 window 是根 window:
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new UI.NavigationRootWindow();
m_window.Activate();
}
private Window m_window;
现在,在 NavigationRootWindow 的 cs 文件中,我可以导航到我想要的页面文件:
public sealed partial class NavigationRootWindow : Window
{
public NavigationRootWindow()
{
this.InitializeComponent();
rootFrame.Navigate(typeof(MainMenu));
}
}
其中 MainMenu 是一个页面元素 :) 现在,在页面元素中,您只需调用 this.Frame 即可获取当前框架 - 这使您可以导航到另一个页面,甚至可以来回导航。一个例子:
private void ConnectButton_OnClick(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(SecondMenu));
}
我不确定这是最佳做法,但它确实有效:D
可能最好的方法是创建一个 Frame
对象并将其分配给 m_window
,而不是有一个专用的 XAML window。我想你可以简单地这样做:
this.m_window = new Window();
this.m_window.Content = rootFrame = new Frame(); ;
this.m_window.Activate();
rootFrame.Navigate(typeof(MainPage));
您需要将 MainWindow
重命名为 MainPage
并将其根元素类型更改为 Page
.
在您的 App
class 中,您可以公开委托给 rootFrame
的 Navigate
的 public Navigate
方法。这样一来,您将能够使用 App.Current
从应用程序中的任何位置获取应用程序实例,从而使应用程序的根框架在后退堆栈中向前导航。
或者,制作类似 Prism 的导航服务,以在整个应用程序中提供导航服务。
我正在尝试进行简单的页面导航,但我找不到任何有关如何在 WinUI 3.0 中进行此操作的文档。
目前,当我使用 WinUI 3.0 创建空白应用程序时,我在 App.xaml.cs
中创建了以下代码 protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
}
private Window m_window;
虽然在我在网上找到的许多其他示例中,根框架是在上面的 OnLaunched 事件中定义的。
我要如何定义MainWindow.xaml或App.xaml才能得到一个可以在Page1.xaml和Page2.xaml之间自由切换的框架?
编辑: 我现在发现我可以通过调用来检索框架:
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
Frame rootFrame = m_window.Content as Frame;
m_window.Activate();
rootFrame.Navigate(typeof(UI.MainMenu));
}
但是导航失败并出现 System.NullReferenceException: 'Object reference not set to an instance of an object.'
错误。我做错了什么 :S?
所以,我设法找到了一种方法。
首先,我创建了一个 window,我将其称为 NavigationRootWindow。
<Window
x:Class="Whosebug.UI.NavigationRootWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Whosebug.UI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Frame x:Name="rootFrame"/>
</Grid>
在这里,我添加了一个名为 rootFrame 的框架。
现在,在应用程序中,我将 onLaunched 函数定义为自动生成的,但我确保添加的 window 是根 window:
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new UI.NavigationRootWindow();
m_window.Activate();
}
private Window m_window;
现在,在 NavigationRootWindow 的 cs 文件中,我可以导航到我想要的页面文件:
public sealed partial class NavigationRootWindow : Window
{
public NavigationRootWindow()
{
this.InitializeComponent();
rootFrame.Navigate(typeof(MainMenu));
}
}
其中 MainMenu 是一个页面元素 :) 现在,在页面元素中,您只需调用 this.Frame 即可获取当前框架 - 这使您可以导航到另一个页面,甚至可以来回导航。一个例子:
private void ConnectButton_OnClick(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(SecondMenu));
}
我不确定这是最佳做法,但它确实有效:D
可能最好的方法是创建一个 Frame
对象并将其分配给 m_window
,而不是有一个专用的 XAML window。我想你可以简单地这样做:
this.m_window = new Window();
this.m_window.Content = rootFrame = new Frame(); ;
this.m_window.Activate();
rootFrame.Navigate(typeof(MainPage));
您需要将 MainWindow
重命名为 MainPage
并将其根元素类型更改为 Page
.
在您的 App
class 中,您可以公开委托给 rootFrame
的 Navigate
的 public Navigate
方法。这样一来,您将能够使用 App.Current
从应用程序中的任何位置获取应用程序实例,从而使应用程序的根框架在后退堆栈中向前导航。
或者,制作类似 Prism 的导航服务,以在整个应用程序中提供导航服务。