How to display a content Dialog box and a input Dialog box one after the other?

Abhishek Jakabal 86 Reputation points
2021-09-08T12:19:28.3+00:00

I am actually displaying something in a Content Dialog box which is coinciding with another Input Dialog box where the user enters some text data. So the first Content Dialog box waits for the user to press 'OK' and the second Input Dialog box waits for the user to enter the data into it. Since these both are trying to execute at a same time, the GUI/UWP app crashes suddenly. Please provide a solution for it. I don't want the application to crash.

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Roy Li - MSFT 31,551 Reputation points Microsoft Vendor
    2021-09-09T09:41:09.47+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Since your scenario is different to reproduce, I made a simple test. I have a ContentDialog shown when I click a button. The button click will also trigger a timer which will open another ContentDialog after 5s. The result is that I received an unhandled exception telling that An async operation was not properly started. Only a single ContentDialog can be open at any time. This should be the same with you. So what we need to do is to prevent the second ContentDialog open before the first one is closed.

    My solution is that using a flag( I use a Boolean value in my test) to indicate if there is a ContentDialog showing. Before you need to show a ContentDialog, check the flag using the While keyword. This makes sure the ContentDialog will open after the previous is closed. Like this:

              // check the flag  
                while (!flag)  
                {  
                    ContentDialog ....      
                    //some code here  
                    Isdialogopen = true;  
                    ContentDialog.ShowAsync();  
                }  
    

    I made a simple demo and you could take a look at it. Then you could use it in your project.

    Code-behind:

     //use a boolean as the flag  
            bool Isdialogopen { get; set; }  
      
            public MainPage()  
            {  
                this.InitializeComponent();  
                Isdialogopen = false;  
            }  
      
            private async void Button_Click(object sender, RoutedEventArgs e)  
            {  
                DispatcherTimer dispatcherTimer = new DispatcherTimer();  
                dispatcherTimer.Tick += DispatcherTimer_Tick; ;  
                dispatcherTimer.Interval = new TimeSpan(0, 0, 3);  
                dispatcherTimer.Start();  
      
                // check the flag  
                while (!Isdialogopen)  
                {  
                    ContentDialog no_scan1 = new ContentDialog  
                    {  
                        Content = "Start feeding... ",  
                        PrimaryButtonText = "OK"  
                    };  
                    // handle the button click event to reset the flag  
                    no_scan1.PrimaryButtonClick += No_scan1_PrimaryButtonClick;  
                    Isdialogopen = true;  
                    ContentDialogResult result = await no_scan1.ShowAsync();  
                }  
            }  
      
      
            private async void DispatcherTimer_Tick(object sender, object e)  
            {  
                while (!Isdialogopen)   
                {  
                    ContentDialog no_scan1 = new ContentDialog  
                    {  
                        Content = "new ",  
                        PrimaryButtonText = "OK"  
                    };  
                    // handle the button click event to reset the flag  
                    no_scan1.PrimaryButtonClick += No_scan1_PrimaryButtonClick;  
                    Isdialogopen = true;  
                    ContentDialogResult result = await no_scan1.ShowAsync();  
                }  
                 
            }  
      
            private void No_scan1_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)  
            {  
                //reset the flag  
                Isdialogopen = false;  
            }  
    

    The result of this demo is that when you click a button, the first ContentDialog will show, and if you click the OK button and dismiss the first ContentDialog, then the second ContentDialog will show.

    Please feel free to ask if you still have questions.

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful