【发布时间】:2018-03-15 06:05:49
【问题描述】:
如果屏幕(例如设置屏幕)有任何更改,我会在离开屏幕时尝试向用户显示一个对话框。然而。 DisplayAlert 方法需要等待才能获得我需要用作 OnBackButtonPressed 中的返回值的结果。我需要等待对话框的结果,然后执行 OnBackButtonPressed 方法的其余部分。
我试图最终执行的代码应该类似于下面的代码:
public partial class MainPage : ContentPage
{
private TestViewModel _vm;
public MainPage()
{
InitializeComponent();
_vm = new TestViewModel();
BindingContext = _vm;
}
protected override bool OnBackButtonPressed()
{
if (_vm.IsUnchanged) return base.OnBackButtonPressed();
var result = await DisplayAlert("Title", "Are you sure you want to leave the screen with unsave changes?", "Yes", "No");
// returning false will exit the screen
return !result;
}
}
public class TestViewModel
{
// this condition logic will evaluate if the user has made any changes to this screen and not saved them by pressing a "Save" button
public bool IsUnchanged{ get; set; }
public TestViewModel()
{
}
}
【问题讨论】:
-
所以从重复问题接受的答案不起作用?
-
不,在我的情况下它不起作用,因为我需要返回对话的结果
标签: c# xamarin.forms async-await