Xamarin Forms-将函数绑定到StackLayout TapGesture



在我看来,我有:

<ContentView.BindingContext>
<vm:HomeViewModel />
</ContentView.BindingContext>

在这个视图模型中,我有:

public void test(object sender, EventArgs e)
{
var x = 0;
}

这只是为了测试,这样我就可以达到断点,但我似乎无法将函数绑定到视图:

<StackLayout BackgroundColor="White">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding test }" />
</StackLayout.GestureRecognizers>

我也试着把它和Tapped属性连接起来,添加括号,我一直得到:

严重性代码描述项目文件行禁止显示状态错误位置57:51。在类型上找不到带有正确签名的方法OnTapGestureRecognizerTapped

我知道我在这里错过了一些东西,有人能解释一下我的问题吗?

您需要在视图模型中绑定一个命令到:

public ICommand cmdTest { get { return new Command(() => test())

现在,在您的视图中,您可以绑定到cmdTest:

<TapGestureRecognizer Command="{Binding cmdTest}" />

最新更新