question

AbhishekJakabal-9046 avatar image
0 Votes"
AbhishekJakabal-9046 asked RoyLi-MSFT commented

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

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.

windows-uwpwindows-uwp-xamlwindows-uwp-runtime
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you please share the code that you are using so that I could try to reproduce your issue? And please share the expected behavior you want to get as well so that I could help to find out a solution based on your code.

0 Votes 0 ·

This is the code for Dialog box that displays a message:
private async void pls_wait_dialog()
{
ContentDialog no_scan1 = new ContentDialog
{
Content = "Start feeding... ",
PrimaryButtonText = "OK"
};
ContentDialogResult result = await no_scan1.ShowAsync();

}
This is the code for the input dialog box where the user enters a password:
private async void Button_Click(object sender, RoutedEventArgs e)
{
if(slider_status == 0)
{
reset_slider.Content = "CHANGE";
volumeSlider.IsEnabled = false;
slider_status = 1;
}
else if(slider_status == 1)
{

             string txt = await InputTextDialogAsync1_pswd("Enter the password");
             if (txt == "ggcl456" || txt == "ggcl123")
             {
                 reset_slider.Content = "SAVE";
                 volumeSlider.IsEnabled = true;
                 slider_status = 0;
             }
             else
             {
                 No_scan1();
             }
         }

}

So there is a scenario where these two dialog boxes try to pop up at the same time or one of these, try to pop-up when the other one is already open. So in these scenarios, the application tends to crash, which is not acceptable.

Expected Behavior:
I want one of these dialog boxes to wait until the other one is closed.







0 Votes 0 ·

If you feel that there could be another suitable solution for it, then please do suggest.

0 Votes 0 ·
Show more comments

1 Answer

RoyLi-MSFT avatar image
0 Votes"
RoyLi-MSFT answered RoyLi-MSFT commented

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.





· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please give me some time, i'll try this and tell you the result.

0 Votes 0 ·
RoyLi-MSFT avatar image RoyLi-MSFT AbhishekJakabal-9046 ·

@AbhishekJakabal-9046 Sure, please feel free to come back if you still have questions.

1 Vote 1 ·

@AbhishekJakabal-9046 Any updates about this issue?

0 Votes 0 ·