Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Only a single ContentDialog can be open at any time." error while opening another contentdialog

Tags:

c#

wpf

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?

like image 750
tavier Avatar asked Dec 02 '22 16:12

tavier


1 Answers

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

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.

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.

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

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.

Example:

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);
like image 78
William Bradley Avatar answered Dec 21 '22 01:12

William Bradley