【发布时间】:2020-03-02 10:51:42
【问题描述】:
我正在运行一个包含 2 个页面的 UWP 应用。在 MainPage 中,我启动了一个 ContentDialog Async 方法,该方法在框架中打开 page2。在 page2 中,我有一个辅助 ContentDialog,我通过按一个按钮打开它。 在 Button_Click 方法中,我想关闭正在运行的 Async() 方法并等待启动下一个方法,直到我 100% 确定第一个方法已关闭。
我现在遇到的问题是,如果我打开第二个ContentDialog的按钮太快,程序就会崩溃。
我尝试过线程休眠,但这只会延迟问题。
主页(客户信息页):
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 打开后立即单击它,不要太快调用它。
【问题讨论】: