locked
Exception while opening multiple MessageDialog.

    Question

  • Hi,

    In windowsRT facing some issue while opening multiple MessageDialog one after another. it throws AccessDenied Exception.

    I have searched in forum and also got some solution but they are not generic. 

    Can anyone provide me some MessageDialogHelper which can handle all the Dialog from through out the app.

    like : If any MessageDialog already available on UI thread then add upcoming Dialog to queue and open it once first one has been dismissed.

    thanks



    Ravindra

    Wednesday, March 26, 2014 11:40 AM

Answers

  • this what you looking for?

    protected async override void OnNavigatedTo(NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
    
                var d1 = new MessageDialog("dialog1");
                var t1 = d1.OpenMessageDialogQueued();
    
                var d2 = new MessageDialog("dialog2");
                var t2 = d2.OpenMessageDialogQueued();
    
                var d3 = new MessageDialog("dialog3");
                var t3 = d3.OpenMessageDialogQueued();
    
                var d4 = new MessageDialog("dialog4");
                var t4 = d4.OpenMessageDialogQueued();
    
                await t1;
                await t2;
                await t3;
                await t4;
            }
        }
    
        public static class MessageDialogHelper
        {
            private static bool handlingDialog = false;
            private readonly static Queue<QueuedDialog> _dialogs = new Queue<QueuedDialog>();
    
            public static Task<IUICommand> OpenMessageDialogQueued(this MessageDialog dialog)
            {
                var queuedDialog = new QueuedDialog(dialog);
                _dialogs.Enqueue(queuedDialog);
                if (!handlingDialog)
                {
                    ShowNextDialog();
                }
                return queuedDialog.CompletionSource.Task;
            }
    
            private async static void ShowNextDialog()
            {
                try
                {
                    handlingDialog = true;
                    while (_dialogs.Count > 0)
                    {
                        var dialog = _dialogs.Dequeue();
                        var nextDialog = await dialog.ShowAsync();
                    }
                }
                finally
                {
                    handlingDialog = false;
    
                }
            }
    
            private class QueuedDialog
            {
                private MessageDialog dialog;
    
                public QueuedDialog(MessageDialog dialog)
                {
                    this.dialog = dialog;
                    CompletionSource = new TaskCompletionSource<IUICommand>();
                }
    
                public TaskCompletionSource<IUICommand> CompletionSource { get; private set; }
    
                public async Task<IUICommand> ShowAsync()
                {
                    var result = await dialog.ShowAsync();
                    CompletionSource.SetResult(result);
                    return result;
                }
            }
        }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, March 26, 2014 12:10 PM