使用按钮单击Winui 3中的“更改视图” 3

发布于 2025-02-09 22:56:14 字数 1156 浏览 1 评论 0 原文

在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

从模板中可以使用,几乎没有更改任何内容。在尝试模板之前,我首先在自己的代码中尝试并遇到相同的错误。有什么方法可以更改按钮点击时的视图?

In WinUI 3 I want to change the view to a SecondaryView after a button click. The view change works flawlessly if I just add it to my code. But as soon as it happens in a Button Click function the app crashes. I am using the Template Studio for WinUI template to do this. The relative code is as follows:

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
}

The exception I get is

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
     UnhandledException += (sender, e) =>
     {
          if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
     };
#endif

This is all right from the template, barely changing anything. I tried it in my own code first before trying the template and got the same error. Is there any way to change the view on a button click?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安稳善良 2025-02-16 22:56:14

您需要初始化 _navigationservice

public sealed partial class MainPage : Page
{
    private readonly INavigationService _navigationService;

    public MainViewModel ViewModel
    {
        get;
    }

    // Constructor
    public MainPage()
    {
        ViewModel = App.GetService<MainViewModel>();
        InitializeComponent();
        _navigationService = App.GetService<INavigationService>();  // This line is missing.
        _navigationService.NavigateTo(typeof(SecondaryViewModel).FullName);
    }

    private void Button_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
    {
        _navigationService.NavigateTo(typeof(SecondaryViewModel).FullName);
    }
}

You need to initialize the _navigationService.

public sealed partial class MainPage : Page
{
    private readonly INavigationService _navigationService;

    public MainViewModel ViewModel
    {
        get;
    }

    // Constructor
    public MainPage()
    {
        ViewModel = App.GetService<MainViewModel>();
        InitializeComponent();
        _navigationService = App.GetService<INavigationService>();  // This line is missing.
        _navigationService.NavigateTo(typeof(SecondaryViewModel).FullName);
    }

    private void Button_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
    {
        _navigationService.NavigateTo(typeof(SecondaryViewModel).FullName);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文