kdn29
kdn29

Reputation: 15

Queuing ContentDialog in uwp

It is possible to queue ContentDialog's and show it after another is closed? I used this to find ContentDialog

var popups = VisualTreeHelper.GetOpenPopups(Window.Current);
        int i = 0;
        foreach (var item in popups)
        {
            if (item.Child is ContentDialog)
                i++;
        }
        if (i == 0)
        {
            //show ContentDialog
        }
        else
            // add to my listOfContentDialog

But if I've try to open more ContentDialog's at once it throw an exception that operation is started incorrectly.

UPDATE According to Jayden Gu - MSFT my working code

private List<string> testowa_lista = new List<string>();

    private async void Komunikat_siatki(string v)
    {
        if(mozeWyskoczyc)
        {
            mozeWyskoczyc = false;
            //my internal code to generate ContentDialog using v string
            testowa_lista.Remove(v);
            mozeWyskoczyc = true;
            if (testowa_lista.Count > 0)
            {
                var i = testowa_lista.First();
                Komunikat_siatki(i);
            }                    
        }
        else
        {
            testowa_lista.Add(v);
        }
    }

Upvotes: 0

Views: 1226

Answers (5)

binaryfunt
binaryfunt

Reputation: 7137

I've created a function that replicates the functionality of the JavaScript Alert() function. It creates a ContentDialog and adds this to a queue. An event handler ensures that when the Dialog is closed, it removes itself from the queue and calls ShowAsync() for the next Dialog in the queue, assuming there is one.

sealed partial class App : Application
{
    ...
    private static List<ContentDialog> DialogQueue { get; } = new List<ContentDialog>();

    public static async void Alert(string text)
    {
        var Dialog = new ContentDialog()
        {
            Title = text,
            CloseButtonText = "OK"
            ...
        };
        App.DialogQueue.Add(Dialog);

        // Add event handler for when dialog closed:
        Dialog.Closed += Dialog_Closed;

        if (App.DialogQueue.Count == 1) // Only one in queue
        {
            await Dialog.ShowAsync();
        }
    }
    // Event handler for when dialog closed:
    private static async void Dialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
    {
        App.DialogQueue.Remove(sender);
        if (App.DialogQueue.Count > 0)
        {
            await App.DialogQueue[0].ShowAsync();
        }
    }
}

I've put the static function inside App.xaml.cs (along with the queue and dialog closed event handler), so you call it like App.Alert("Hello world") although you could put it inside its own class. Of course, you can add parameters to Alert() to populate the different properties of the ContentDialog.

Upvotes: 2

Pratyay
Pratyay

Reputation: 1301

Since you need to queue multiple content dialogs you may try this :

            int count = 1;  //count is used to simply to have dynamic content in the dialogs
            private async void startCreatingDialog()
            {
                var result = await createDialog(count.ToString()).ShowAsync();
                if (result == ContentDialogResult.Primary)
                {
                    //create a new dialog based on user's input 
                    count++;
                    startCreatingDialog();
                }

            }
            private ContentDialog createDialog(string content)
            {
                ContentDialog contentDialog = new ContentDialog()
                {                
                    Content = content,
                    //clicking on the primarybutton will create the next ContentDialog
                    PrimaryButtonText = "Show another dialog",                        
                    SecondaryButtonText = "Cancel"
                };

                return contentDialog;

            }

To start showing the dialog queue you need to call startCreatingDialog() and rest will be handled according the user's choice..

Hope this helps..

Upvotes: 0

Jayden
Jayden

Reputation: 3286

Only one ContentDialog can be shown at a time.

If one ContentDialog is shown, we can not show another ContentDialog. When you add the ShowAsync method in the LostFocus event of the TextBox and use Tap button, it will show two ContentDialog at same time.

You should be able to add a bool field to save to state of if the ContentDialog can be shown.

For example:

private async void Text_LostFocus(object sender, RoutedEventArgs e)
{
    if (dialogCanShow)
    {
        dialogCanShow = false;
        var textBox= sender as TextBox;
        var contentDialog = new ContentDialog();
        contentDialog.Title = textBox.Name;
        contentDialog.CloseButtonText = "Close";
        await contentDialog.ShowAsync();
        dialogCanShow = true;
    }
}

When we close the ContentDialog, the cursor will show in the current TextBox.

Upvotes: 0

Marian Dolinsk&#253;
Marian Dolinsk&#253;

Reputation: 3492

You may store them in some collection and then show them one after one:

List<ContentDialog> dialogs = new List<ContentDialog>()
{
    contentDialog1,
    contentDialog2,
    contentDialog3
};

foreach (ContentDialog dialog in dialogs)
{
    await dialog.ShowAsync();
}

Upvotes: 0

Xeorge Xeorge
Xeorge Xeorge

Reputation: 462

you can use a message dialog instead.

await new MessageDialog("First").ShowAsync();

await new MessageDialog("Second").ShowAsync();

Upvotes: 0

Related Questions