Xamarin.Forms MVVM 从 DISplayAlert 得到响应 [英] Xamarin.Forms MVVM getting responce from DIsplayAlert
本文介绍了Xamarin.Forms MVVM 从 DISplayAlert 得到响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 MVVM 视图正确使用 DisplayAlert()
?
How to work with DisplayAlert()
correctly from MVVM view?
我有一些屏幕和视图模型.在 ViewModel 中,我定义了一些 Actions
:
I have some screen and ViewModel for it.
Inside the ViewModel I defined some Actions
:
class PageViewModel : INotifyPropertyChanged
{
public Action DisplayDataSavedPromt;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public ICommand SaveCommand { protected set; get; }
public PageViewModel()
{
SaveCommand = new Command(OnSubmit);
}
public void OnSubmit()
{
DisplayDataSavedPromt();
}
}
然后我从 View Page.cs
调用它:
And then I'm calling it from View Page.cs
:
public Page ()
{
var vm = new PageViewModel();
this.BindingContext = vm;
vm.DisplayDataSavedPromt += () => DisplayAlert("Success", "Your data are saved", "OK");
InitializeComponent ();
}
它工作正常,但我不明白如何将某些操作绑定到此警报的按钮?例如.当用户在 Alert 中点击 OK 时,打开新页面.
And it's working fine, but I can't understand how can I bind some actions to this Alert's buttons? E.g. when user clicks OK inside Alert, open new page.
我应该在 ViewModel
中以某种方式还是在 View 中执行此操作?
Should I do it inside ViewModel
somehow or inside View?
推荐答案
你可以在你的视图模型中试试这个
You can try this in your view model
var res = await App.Current.MainPage.DisplayAlert("Success", "Your data are saved", "Ok", "Cancel");
if(res){//logic} else {//logic}
这篇关于Xamarin.Forms MVVM 从 DISplayAlert 得到响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文