【发布时间】:2017-07-07 16:34:22
【问题描述】:
在视图模型中我有一个命令,在视图中我有一个绑定到该命令的按钮:
...
public Command SaveCommand { get; }
private async void Save(){
IsBusy = true;
Estatus = "Grabando...";
Task<bool> taskSaved = _negotiationRepo.SaveNegotiation(_negotiation, _following);
bool Saved = await taskSaved;
IsBusy = false;
if (Saved)
{
Estatus = "Saved";
await Application.Current.MainPage.DisplayAlert("Negotiation","Saved correctly","OK");
}
else
{
await Application.Current.MainPage.DisplayAlert("Negotiation", "Problem saving negotiation", "OK");
}
}
public NegotiationVM(Client client){
...
SaveCommand = new Command((Save),canExecute: ()=>!IsBusy);
...
}
在 XAML 视图中:
<Button Text="Save????" BackgroundColor="#3276b1" TextColor="White" Command="{Binding SaveCommand}">
如您所见,我在这个 ViewModel 类中有一个 DisplayAlert。我不确定,但根据 MVVM,我不应该在这里这样做,或者至少不应该那样做。 触发此命令后如何正确显示警报或提示?
目前我没有使用任何 MVVM 框架,大多数示例都集中在 MVVM Light 或 Prism 中。
【问题讨论】:
-
不相关,这绝对是你应该如何在 MVVM 中处理这种类型的事情,并带有一个好的代码示例。
-
@Will 为什么被标记为重复?这适用于 Xamarin.Forms 而不是 WPF。即使是相似的,也不等于功能和实现。
-
我不知道xamarin,所以不知道他们有没有内置的机制,所以重新打开。如果他们不,那个骗子就有你的答案。 xamarin 不会阻止您定义一个接口来模拟与对话框的交互,然后在您的 UI 中实现它并将实现传递到您的视图模型中。
标签: c# xaml mvvm xamarin.forms