如何使 UWP ContentDialog 真正成为模态

How to make UWP ContentDialog truly modal

我在 UWP 应用程序中有一个导航视图模式,其中包含一个托管子页面框架的“导航根”页面。如果我从子页面调用 ContentDialog,如果我使用键盘快捷键,我仍然可以访问母版页面中的对象。如果打开另一个内容对话框,这很容易导致应用程序崩溃。

如何让 ContentDialog 成为真正的模态?

可以在此处找到演示该问题的项目:https://github.com/under3415/NavigationApp

简而言之,创建两个页面,一个在框架中托管另一个

<Frame Grid.Row="1" x:Name="RootContentFrame"/>

在母版页中,定义一个 Button 或另一个带有 AccessKey 的对象。 在子页面中,调用 ContentDialog。当内容对话框打开时,按 ALT 键,然后按访问键。即使模态对话框打开,后面的对象也会触发。

In a master page, have a Button or another object with AccessKey defined. In a child page, call a ContentDialog. While content dialog is open press ALT key and then the access key. Even though the modal dialog is open, the object behind fires.

ContentDialog 显示时,它将阻止与应用 window 的交互,直到被明确关闭。但它无法阻止访问键,因为键盘快捷键可以通过为用户提供一种直观的方式来快速导航和与应用程序的 可见 UI 通过键盘 而不是指针设备(例如触摸或鼠标)。

对于您的场景,我们建议创建事件来检测是否显示对话框,然后设置根页面 IsEnable true 或 false。

在您的应用中执行操作 class

public static Action<bool> IsDialogOpen;

检测对话框打开或关闭。

private async void Button_Click(object sender, RoutedEventArgs e)
{

    ContentDialog dialog = new ContentDialog
    {
        Content = "Press ALT, then C or V",
        Title = "Non Modal Dialog",
        PrimaryButtonText = "OK"
    };

    dialog.Opened += Dialog_Opened;
    dialog.Closed += Dialog_Closed;
    _ = await dialog.ShowAsync();


}

private void Dialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
{

    App.IsDialogOpen(false);
}

private void Dialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{

    App.IsDialogOpen(true);
}

禁用或启用根页面基础对话框是否打开。

public NavigationRoot()
{
    this.InitializeComponent();
    App.IsDialogOpen = (s) =>
    {
        this.IsEnabled = s ? false : true;
  
    };
}