“任何时候都只能打开一个ContentDialog。”打开另一个内容对话框时出错 [英] "Only a single ContentDialog can be open at any time." error while opening another contentdialog

查看:65
本文介绍了“任何时候都只能打开一个ContentDialog。”打开另一个内容对话框时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Windows.UI.Xaml.Controls.ContentDialog 显示确认。根据第一个对话框的响应,我将(或不会)显示另一个对话框。但是,当我尝试打开第二个内容对话框时,它会抛出:任何时候都只能打开一个ContentDialog。错误。即使在用户界面中,第一个对话框也将关闭,但是以某种方式我仍然无法打开第二个对话框。知道吗?

I am using Windows.UI.Xaml.Controls.ContentDialog to show a confirmation. And based on the response from the first dialog I would (or would not) show another dialog. But, when I am trying to open the second content dialog it throws : "Only a single ContentDialog can be open at any time." error. Even though in the UI, first dialog would be closed but somehow I am still not able to open the second dialog. Any idea?

推荐答案

我已经创建了一些代码来处理我的Apps中的这种难题:

I have created some code to handle this type of conundrum in my Apps:

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)
            {
                await DialogAwaiter.Task;
                DialogAwaiter = new TaskCompletionSource<bool>();
            }
            else ActiveDialog.Hide();
        }
        ActiveDialog = Dialog;
        ActiveDialog.Closed += ActiveDialog_Closed;
        await ActiveDialog.ShowAsync();
        ActiveDialog.Closed -= ActiveDialog_Closed;
    }

    public static ContentDialog ActiveDialog;
    static TaskCompletionSource<bool> DialogAwaiter = new TaskCompletionSource<bool>();
    private static void ActiveDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args) { DialogAwaiter.SetResult(true); }
}

要使用这些方法,您需要创建ContentDialog及其内容

To use these Methods, you need to create the ContentDialog and its content in a variable, then pass the variable, and bool into the Method.

使用CreateContentDialogAsync(),如果您需要在应用程序代码中回调,请说是否有按钮在对话框中,您要等待按钮按下,然后在对话框后的代码中从表单获取值。

Use CreateContentDialogAsync(), if you require a callback in your app code, say if you have a button in your Dialog, and you want wait for a button press, and then get the value from the form in code after the dialog.

如果不需要等待对话框在UI代码中完成,请使用CreateContentDialog()。

Use CreateContentDialog(), if you don't need to wait for the Dialog to complete in your UI Code.

使用awaitPreviousDialog在显示下一个对话框之前等待上一个对话框完成,或者设置为false,以删除上一个对话框,然后显示下一个对话框,例如,如果要显示错误框或下一个对话框

Use awaitPreviousDialog to wait for the previous dialog to complete before showing the next Dialog, or set false, to remove the previous Dialog, then show the next Dialog, say, if you want to show an Error Box, or the next Dialog is more important.

示例:

await ContentDialogMaker.CreateContentDialogAsync(new ContentDialog
{
    Title = "Warning",
    Content = new TextBlock
    {
        Text = "Roaming Appdata Quota has been reached, if you are seeing this please let me know via feedback and bug reporting, this means that any further changes to data will not be synced across devices.",
        TextWrapping = TextWrapping.Wrap
    },
    PrimaryButtonText = "OK"
}, awaitPreviousDialog: true);

这篇关于“任何时候都只能打开一个ContentDialog。”打开另一个内容对话框时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆