在Xamarin中,你把手势命令放在哪里?在视图模型中?
我当前有一个视图模型,如下所示:在Xamarin中,你把手势命令放在哪里?在视图模型中?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我当前有一个视图模型,如下所示: public class PhrasesFrameViewModel : ObservableProperty { bool customPointsSwitch; public PhrasesFrameViewModel() { var aButtonClickedCommand = new Command(() => { App.DB.IncrementScore(Ap
public class PhrasesFrameViewModel : ObservableProperty
{
bool customPointsSwitch;
public PhrasesFrameViewModel()
{
var aButtonClickedCommand = new Command(() =>
{
App.DB.IncrementScore(App.cfs, App.phrase, (int)App.aBtn);
App.correctButtonPressed = (int)App.aBtn;
ResetTimer2();
});
var wordGridClickedCommand = new Command(() =>
{
if (App.Timer1Running)
ResetTimer1();
else
ResetTimer2();
});
}
private static void ResetTimer1()
{
if (App.tokenSource1 != null)
{
App.Timer1Seconds = 0;
App.tokenSource1.Cancel();
}
}
private static void ResetTimer2()
{
if (App.tokenSource2 != null)
{
App.Timer2Seconds = 0;
App.tokenSource2.Cancel();
}
}
public bool CustomPointsSwitch
{
get
{
return customPointsSwitch;
}
set
{
if (value != customPointsSwitch)
{
customPointsSwitch = value;
NotifyPropertyChanged("CustomPointsSwitch");
App.DB.UpdateBoolSetting(Settings.Cp, customPointsSwitch);
}
}
}
我相信大多数视图模型都会有类似于CustomPointsSwitch的代码,但手势识别器和命令的代码以及重置的小方法(视图模型中的其他一些方法使用)如何。这些都属于视图模型还是应该属于另一个类 简短回答:在这种特殊情况下,根据问题中共享的代码,它们属于视图模型 长答案: 视情况而定。如果您的命令处理程序需要与UI交互,它应该保持在视图中;如果是其他内容,即表示或业务逻辑,则应在视图模型中定义 命令是MVVM模式不可分割的一部分,因为它们允许将视图模型与视图分离,从而使单元测试、维护和扩展更容易。它们是视图和视图模型之间通信的推荐通道(数据绑定除外) 在大多数情况下,当视图中的用户交互/事件需要触发视图模型中的各种操作时,使用命令界面——在这些情况下,命令在视图模型本身中定义并作为属性公开