我是MVVM的新手,在我的Xamarin.Forms应用程序中有一些东西,我做错了。
这是我在SessionsPage.xml中的内容
<Grid.Children>
<Image Source="zero.jpg" Grid.Row="0" Grid.Column="0">
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand}"
CommandParameter="0" />
</Image.GestureRecognizers>
我有一个链接到命令“TapCommand”的图像。
在这个视图的构造函数中,我添加了:
tapViewModel = new EasyScrum.Views.TapViewModel();
ViewModel类是:
public class TapViewModel : INotifyPropertyChanged
{
int taps = 0;
ICommand tapCommand;
public event PropertyChangedEventHandler PropertyChanged;
public TapViewModel()
{
// configure the TapCommand with a method
tapCommand = new Command(OnTapped);
}
public ICommand TapCommand
{
get { return tapCommand; }
}
void OnTapped(object s)
{
taps++;
Debug.WriteLine("parameter: " + s);
}
//region INotifyPropertyChanged code omitted
}
事件没有解雇,出了什么问题?
答案 0 :(得分:1)
我自己回答。
我的错误是我必须分配
this.BindingContext = new TapViewModel();