我正在运行一个2页的UWP应用。在MainPage中,我启动一个ContentDialog Async方法,该方法在一个框架中打开page2。在第2页中,我有一个辅助ContentDialog,可以通过按一个按钮来打开它。 在Button_Click方法中,我想关闭正在运行的Async()方法,并等待启动下一个方法,直到我100%确定第一个方法已关闭。
我现在遇到的问题是,如果我在打开第二个ContentDialog的按钮后过快按下它,程序就会崩溃。
我已经尝试使线程休眠,但这只会延迟问题。
主页(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();
}
因此,只要您在打开后立即单击ConfirmationDialog_PrimaryButtonClick的速度不要太快,就可以使用它。