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.