【发布时间】:2020-04-13 14:59:10
【问题描述】:
我想使用 Xamarin Forms 的 OnBackButtonPressed() 内置功能将它们导航到 MainPage 或在回答 DisplayAlert() 时将它们保留在当前页面。
问题是当调用OnBackButtonPressed() 时,我无法等待答案(在本例中为OnAlertYesNoClicked();),并且由于布尔值的默认值为false,它只会关闭应用程序.
有没有办法将用户的答案用作OnBackButtonPressed() 的布尔值?
例子:
bool action;
async public void OnAlertYesNoClicked()
{
bool action = await DisplayAlert("Question?", "Are you sure you want to go back? Your result wont be saved!", "Yes", "No");
}
protected override bool OnBackButtonPressed()
{
OnAlertYesNoClicked();
if(action == true)
{
Navigation.PushModalAsync(new MainPage(), true);
return false;
}
else
{
return true;
}
}
【问题讨论】: