Reputation: 1
I'm running an UWP app with 2 pages. In MainPage I start a ContentDialog Async method which opens page2 in a frame. In page2 I have a secondary ContentDialog which I open by pressing a button. In the Button_Click method I want to close the running Async() method and await to start the next one until I am 100% sure the first one is closed.
The problem I encounter now is that the program crashes if I press the secondary ContentDialog's buttons too fast after it opens.
I've tried thread sleeping but that only delays the problem.
Mainpage(CustomerInfoPage):
transactionContent = new ContentDialog();
Frame transactionFrame = new Frame();
transactionFrame.Navigate(typeof(TransactionPage), selectedAccount);
transactionContent.Content = transactionFrame;
transactionContent.ShowAsync();
Page2(TransactionPage):
private async void DepositButton_ClickAsync(object sender, RoutedEventArgs e)
{
CustomerInfoPage.transactionContent.Hide();
ContentDialog confirmationDialog = new ContentDialog
{
Title = "Deposit Funds.",
Content = $"You will deposit {depositTextBox.Text} SEK.\nYour new balance will be: {ReceivedAccount.Balance + deposit} SEK",
PrimaryButtonText = "CONFIRM",
SecondaryButtonText = "CANCEL"
};
confirmationDialog.PrimaryButtonClick += ConfirmationDialog_PrimaryButtonClick;
await confirmationDialog.ShowAsync();
async void ConfirmationDialog_PrimaryButtonClick(ContentDialog _sender, ContentDialogButtonClickEventArgs args)
{
confirmationDialog.Hide();
}
CustomerInfoPage.transactionContent.ShowAsync();
}
So this works as long as you don't call the ConfirmationDialog_PrimaryButtonClick too fast by clicking on it instantly after it is opened.
Upvotes: 0
Views: 726
Reputation: 1882
As @Richard Zhang - MSFT said in previous answer
So you can't start the second CotnentDialog while the first ContentDialog is still running.
If you wish to close the ContentDialog
from previous page right before opening new ContentDialog
you can use the code from below which will close any ContentDialog
which is currently open.
var openedpopups = VisualTreeHelper.GetOpenPopups(Window.Current);
foreach (var popup in openedpopups)
{
if(popup.Child is ContentDialog)
{
(popup.Child as ContentDialog).hide();
}
}
Upvotes: 0
Reputation: 7737
This is introduced in the UWP documentation about Dialog.
There can only be one ContentDialog open per thread at a time. Attempting to open two ContentDialogs will throw an exception, even if they are attempting to open in separate AppWindows.
So you can't start the second CotnentDialog
while the first ContentDialog
is still running.
Best regards.
Upvotes: 0