Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xamarin表单-将TapGestureRecognitor绑定到代码隐藏,而不是视图模型_C#_Xaml_Xamarin.forms - Fatal编程技术网

C# xamarin表单-将TapGestureRecognitor绑定到代码隐藏,而不是视图模型

C# xamarin表单-将TapGestureRecognitor绑定到代码隐藏,而不是视图模型,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我意识到这打破了MVVM模式,但是否可以将TapGestureRecognitor绑定到XAML.cs代码中的方法,而不是视图模型 <Image.GestureRecognizers> <TapGestureRecognizer Command="{Binding Path=BindingContext.SetImageCommand, Source={x:Reference ThePage}}" CommandParamete

我意识到这打破了MVVM模式,但是否可以将TapGestureRecognitor绑定到XAML.cs代码中的方法,而不是视图模型

  <Image.GestureRecognizers>
      <TapGestureRecognizer 
        Command="{Binding Path=BindingContext.SetImageCommand, Source={x:Reference ThePage}}"
        CommandParameter="{Binding .}"/>
  </Image.GestureRecognizers>

当然,只需使用
点击事件

<TapGestureRecognizer Tapped="Thing_Tapped" />
也许,再看一眼,您的意思是如果您可以将
命令
绑定到代码背后的某些内容。我还没有对它进行测试,但看起来它应该可以与您的代码配合使用,只需稍作调整即可:

<Image.GestureRecognizers>
      <TapGestureRecognizer Command="{Binding Path=SetImageCommand, Source={x:Reference ThePage}}" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>

注意我是如何从绑定中删除了
BindingContext.
。这意味着它绑定到了页面的
BindingContext
属性。当然,您也可以绑定到其他属性。现在,您只需将
SetImageCommand
移动到页面后面的代码,它就可以工作了


无论哪种方式,现在都可以从页面而不是视图模型触发逻辑。

当然,只需使用
点击事件

<TapGestureRecognizer Tapped="Thing_Tapped" />
也许,再看一眼,您的意思是如果您可以将
命令
绑定到代码背后的某些内容。我还没有对它进行测试,但看起来它应该可以与您的代码配合使用,只需稍作调整即可:

<Image.GestureRecognizers>
      <TapGestureRecognizer Command="{Binding Path=SetImageCommand, Source={x:Reference ThePage}}" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>

注意我是如何从绑定中删除了
BindingContext.
。这意味着它绑定到了页面的
BindingContext
属性。当然,您也可以绑定到其他属性。现在,您只需将
SetImageCommand
移动到页面后面的代码,它就可以工作了


无论哪种方式,您现在都可以从页面而不是视图模型中触发逻辑。

添加了更多,以防您仍然想使用命令:-),谢谢!如果你不介意的话,还有一件事吗?如果我使用了Tapped选项,我可以将Tapped对象作为参数传递给被调用的代码吗?不太容易。
sender
参数应该是
TapGestureRecognitor
。因此,您可以执行以下操作:
((TapGestureRecognitizer)sender)。CommandParameter
,前提是您仍然将值放入
CommandParameter
属性中。如果您仍然想使用命令,请添加多一点:-),谢谢!如果你不介意的话,还有一件事吗?如果我使用了Tapped选项,我可以将Tapped对象作为参数传递给被调用的代码吗?不太容易。
sender
参数应该是
TapGestureRecognitor
。因此,您可以执行如下操作:
((TapGestureRecognitizer)sender).CommandParameter
,前提是您仍然将值放在
CommandParameter
属性中。