使用按钮单击Winui 3中的“更改视图” 3
在Winui 3中,我想将视图更改为 SecondaryView
单击按钮后。如果我只将其添加到代码中,则视图更改可完美。但是,一旦发生在按钮单击功能中,应用程序就会崩溃。我正在使用Winui 模板使用模板工作室来执行此操作。相对代码如下:
mainpage.xaml:
<Grid x:Name="ContentArea">
<TextBlock Text="Main Page"/>
<Button Content="Press" Click="Button_Clicked"/>
</Grid>
mainpage.xaml.cs
private readonly INavigationService _navigationService;
public MainPage()
{
ViewModel = App.GetService<MainViewModel>();
InitializeComponent();
_navigationService.NavigateTo(typeof(SecondaryViewModel).FullName); // WORKS
}
private void Button_Clicked(object sender, RoutedEventArgs e)
{
_navigationService.NavigateTo(typeof(SecondaryViewModel).FullName); // DOESN'T WORK
}
我得到的例外是,
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
从模板中可以使用,几乎没有更改任何内容。在尝试模板之前,我首先在自己的代码中尝试并遇到相同的错误。有什么方法可以更改按钮点击时的视图?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要初始化
_navigationservice
。You need to initialize the
_navigationService
.