简体   繁体   English

如何使 UWP ContentDialog 真正模态化

[英]How to make UWP ContentDialog truly modal

I have a navigation view pattern in UWP app with a "navigation root" page hosting a frame for child pages.我在 UWP 应用程序中有一个导航视图模式,其中“导航根”页面托管子页面的框架。 If I call a ContentDialog from a child page, I can still access objects in the master page if I use keyboard shortcuts.如果我从子页面调用ContentDialog ,如果我使用键盘快捷键,我仍然可以访问母版页中的对象。 This can easily result in an app crash if another content dialog is opened.如果打开另一个内容对话框,这很容易导致应用程序崩溃。

How can I make ContentDialog truly modal?如何使ContentDialog真正成为模态?

Project demonstrating the issue can be found here: https://github.com/under3415/NavigationApp可以在此处找到演示该问题的项目: https://github.com/under3415/NavigationApp

In a nutshell, create two pages, one hosting the other in a frame简而言之,创建两个页面,一个在框架中托管另一个

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

In a master page, have a Button or another object with AccessKey defined.在母版页中,定义一个Button或另一个AccessKey并定义 AccessKey。 In a child page, call a ContentDialog .在子页面中,调用ContentDialog While content dialog is open press ALT key and then the access key.打开内容对话框时,按ALT键,然后按访问键。 Even though the modal dialog is open, the object behind fires.即使模态对话框打开,后面的 object 也会触发。

In a master page, have a Button or another object with AccessKey defined.在母版页中,定义一个 Button 或另一个 object 并定义 AccessKey。 In a child page, call a ContentDialog.在子页面中,调用 ContentDialog。 While content dialog is open press ALT key and then the access key.打开内容对话框时,按 ALT 键,然后按访问键。 Even though the modal dialog is open, the object behind fires.即使模态对话框打开,后面的 object 也会触发。

When the ContentDialog show, it will block interactions with the app window until being explicitly dismissed.ContentDialog显示时,它将阻止与应用程序 window 的交互,直到被显式关闭。 But it could not block access keys, because are keyboard shortcuts that improve the usability and the accessibility of your Windows applications by providing an intuitive way for users to quickly navigate and interact with an app's visible UI through a keyboard instead of a pointer device (such as touch or mouse).但它不能阻止访问键,因为键盘快捷键通过为用户提供一种通过键盘而不是指针设备快速导航和与应用程序的可见 UI交互的直观方式(例如如触摸或鼠标)。

For your scenario, we suggest make event to detect Dialog show or not then set root page IsEnable true or false.对于您的场景,我们建议制作事件来检测对话框是否显示,然后将根页面IsEnable设置为 true 或 false。

Make an Action in your app class在您的应用程序中执行操作 class

public static Action<bool> IsDialogOpen;

Detect Dialog open or close.检测对话框打开或关闭。

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);
}

Disable or Enable Root page base dialog open or not.禁用或启用根页面基础对话框是否打开。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM