【问题标题】:C++/WinRT WinUI3: How to show a ContentDialog before I close the MainWindow?C++/WinRT WinUI3:如何在关闭 MainWindow 之前显示 ContentDialog?
【发布时间】:2022-12-09 23:41:30
【问题描述】:

我试图显示一个 ContentDialog 以确保用户在选择窗口的关闭按钮时确认关闭。但是在 WinUI3 中,我找不到 CloseRequested Event。

我尝试使用AppWindow and Hwnd InteropAppWindow.Closing,但没有用。单击关闭按钮后,什么也没有发生。

【问题讨论】:

    标签: c++ windows-runtime winui-3 c++-winrt windows-app-sdk


    【解决方案1】:

    这是一个基于 Window.Closed event 的解决方案,它有一个我们可以使用的 Handled property

    在 MainWindow.cpp 中:

    namespace winrt::WinUIApp1CPP::implementation
    {
        MainWindow::MainWindow()
        {
            InitializeComponent();
    
            _closing = false;
            Closed([&](IInspectable const&, WindowEventArgs const& e)
                {
                    if (!_closing)
                    {
                        _closing = true;
                        e.Handled(true);
    
                        ContentDialog dialog;
                        dialog.XamlRoot(Content().XamlRoot());
                        dialog.Title(box_value(L"Do you really want to close the app?"));
                        dialog.PrimaryButtonText(L"Yes, close");
                        dialog.CloseButtonText(L"No, cancel");
                        dialog.DefaultButton(ContentDialogButton::Close);
                        dialog.PrimaryButtonClick([&](auto&& ...)
                            {
                                DispatcherQueue().TryEnqueue([&](auto&& ...)
                                    {
                                        Close();
                                    });
                            });
    
                        dialog.ShowAsync().Completed([&](auto&& ...)
                            {
                                _closing = false;
                            });
                    }
                });
        }
    }
    

    使用 MainWindow.h:

    namespace winrt::WinUIApp1CPP::implementation
    {
        struct MainWindow : MainWindowT<MainWindow>
        {
            MainWindow();
            ...
            bool _closing;
            ...
       };
    }
    

    对于它的价值,C# 中的等价物:

    public MainWindow()
    {
        InitializeComponent();
    
        var closing = false;
        Closed += async (s, e) =>
        {
            if (!closing)
            {
                closing = true;
                e.Handled = true;
    
                var dialog = new ContentDialog();
                dialog.XamlRoot = Content.XamlRoot;
                dialog.Title = "Do you really want to close the app?";
                dialog.PrimaryButtonText = "Yes, close";
                dialog.CloseButtonText = "No, cancel";
                dialog.DefaultButton = ContentDialogButton.Close;
                dialog.PrimaryButtonClick += (s, e) => DispatcherQueue.TryEnqueue(Close);
                var result = await dialog.ShowAsync();
                closing = false;
            }
        };
    }
    

    注意:DispatcherQueue.TryEnqueue 的使用不是必需的,但如果没有它,Close() 调用当前会导致 WinUI3 崩溃...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 2013-11-04
      • 1970-01-01
      相关资源
      最近更新 更多