任何时候都只能打开一个ContentDialog出现错误

时间:2019-05-15 21:11:40

标签: uwp

由于这个问题,我继承了一个复杂的C#/ UWP应用程序,该应用程序因崩溃而死机:“任何时候都只能打开一个ContentDialog”。

该应用程序的原始开发者放置了许多对话框,其中许多是针对错误情况的响应,并且显然没有试图跟踪他是否正在尝试在另一个对话框之上打开对话框。

我是否可以通过某种蛮力方式编写可以关闭任何打开的对话框的东西,或者至少可以让我在打开另一个对话框之前检测到这种情况?

我意识到我应该详细了解该应用程序,并尝试找出问题的根本原因。不幸的是,这是一个非常大且非常重要的应用,已经过期了,客户只需要快速修复即可使其正常工作。

2 个答案:

答案 0 :(得分:1)

实际上,推荐的最佳方法是从源代码维护对话框。

如果您真的需要快速修复,请参阅以下来自David的post。使用此方法:VisualTreeHelper.GetOpenPopups(Window)检测打开的弹出窗口,其中也包含您需要的内容对话框。然后执行您想要的操作:

 var popups=VisualTreeHelper.GetOpenPopups(Window.Current);
        foreach (var popup in popups)
        {
            if(popup.Child is ContentDialog)
            {

            }
        }

但是我需要再次澄清,这实际上不是最佳实践,因此最好不要使用VirtualTreeHelper,因为有了源代码。我强烈建议您检查源代码以自己维护所有对话框。

答案 1 :(得分:0)

您可以尝试使用此代码,它对我有用

步骤1:-创建一个类

public static class ContentDialogMaker
    {
        public static async void CreateContentDialog(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
        public static async Task CreateContentDialogAsync(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }

        static async Task CreateDialog(ContentDialog Dialog, bool awaitPreviousDialog)
        {
            if (ActiveDialog != null)
            {
                if (awaitPreviousDialog)
                {
                    ActiveDialog.Hide();
                }
                else
                {
                    switch (Info.Status)
                    {
                        case AsyncStatus.Started:
                            Info.Cancel();
                            break;
                        case AsyncStatus.Completed:
                            Info.Close();
                            break;
                        case AsyncStatus.Error:
                            break;
                        case AsyncStatus.Canceled:
                            break;
                    }
                }
            }
            ActiveDialog = Dialog;
            ActiveDialog.Closing += ActiveDialog_Closing;
            Info = ActiveDialog.ShowAsync();
        }
        public static IAsyncInfo Info;
        private static void ActiveDialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
        {
            ActiveDialog = null;
        }

        public static ContentDialog ActiveDialog;
    }

第2步-调用该类以显示弹出窗口

ContentDialog dialog = new ContentDialog
            {
                Title = title,
                Content = message,
                CloseButtonText = "Ok"
            };
            await ContentDialogMaker.CreateContentDialogAsync(dialog, true);

谢谢!

相关问题