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

前端 未结 6 1433
半阙折子戏
半阙折子戏 2020-12-04 00:29

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 di

6条回答
  •  北海茫月
    2020-12-04 01:10

    I like this answer https://stackoverflow.com/a/47986634/942855, this will allow us ot handle binding all events.

    So extended it a little to check the multiple calls to show dialog.

     private int _dialogDisplayCount;
     private async void Logout_OnClick(object sender, RoutedEventArgs e)
            {
                try
                {    
                    _dialogDisplayCount++;
                    ContentDialog noWifiDialog = new ContentDialog
                    {
                        Title = "Logout",
                        Content = "Are you sure, you want to Logout?",
                        PrimaryButtonText = "Yes",
                        CloseButtonText = "No"
                    };
                    noWifiDialog.PrimaryButtonClick += ContentDialog_PrimaryButtonClick;
                    //await noWifiDialog.ShowAsync();
                    await noWifiDialog.EnqueueAndShowIfAsync(() => _dialogDisplayCount);
                }
                catch (Exception exception)
                {
                    _rootPage.NotifyUser(exception.ToString(), NotifyType.DebugErrorMessage);
                }
                finally
                {
                    _dialogDisplayCount = 0;
                }
            }
    

    modified predicate

    public class Null { private Null() { } }
        public static class ContentDialogExtensions
        {
            public static async Task EnqueueAndShowIfAsync(this ContentDialog contentDialog, Func predicate = null)
            {
                TaskCompletionSource currentDialogCompletion = new TaskCompletionSource();
    
                // No locking needed since we are always on the UI thread.
                if (!CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess) { throw new NotSupportedException("Can only show dialog from UI thread."); }
                var previousDialogCompletion = _previousDialogCompletion;
                _previousDialogCompletion = currentDialogCompletion;
    
                if (previousDialogCompletion != null)
                {
                    await previousDialogCompletion.Task;
                }
                var whichButtonWasPressed = ContentDialogResult.None;
                if (predicate == null || predicate() <=1)
                {
                    whichButtonWasPressed = await contentDialog.ShowAsync();
                }
                currentDialogCompletion.SetResult(null);
                return whichButtonWasPressed;
            }
    
            private static TaskCompletionSource _previousDialogCompletion;
        }
    

提交回复
热议问题