将方法绑定到已点击的ViewCell

时间:2018-08-23 07:00:45

标签: c# xamarin xamarin.forms

是否可以将方法绑定到点击的视单元?

我有以下代码:

<ViewCell Tapped="{Binding SwitchViews}">

哪个抛出以下错误:

Unable to cast object of type 'Xamarin.Forms.Xaml.ElementNode' to type
'Xamarin.Forms.Xaml.ValueNode'.

3 个答案:

答案 0 :(得分:2)

因此,与其将tapped事件提供给viewcell,不如将其分配给viewcell的子元素。

<Viewcell> 
       <Grid>
            <Grid.GestureRecognizers>
                <TapGestureRecognizer Tapped="OpenCaseDetails" />
            </Grid.GestureRecognizers>
            ...
            ...
       </Grid>
</Viewcell>

有关更多信息,请访问https://adityadeshpandeadi.wordpress.com/2018/07/15/the-more-interactive-listview/

,参阅我的博客有关交互式Listview的帖子。

随意来

答案 1 :(得分:0)

您可能在查看模式下没有名为SwitchViews的函数(它可能是一个属性,但这是错误的)。

  

这几乎总是由在XAML中为事件分配值而不是指定方法名称引起的。

     

一个常见的例子是:

<Switch Toggled="{Binding IsToggled}" />
  

已切换是事件的名称。可能要使用的属性称为IsToggled。

(来自https://oliverbrown.me.uk/2017/08/09/solved-unable-to-cast-object-of-type-xamarin-forms-xaml-elementnode-to-type-xamarin-forms-xaml-valuenode/

答案 2 :(得分:0)

好的,我知道了。

我在ViewCell内创建了一个Grid并添加了以下代码:

  <Grid.GestureRecognizers>
       <TapGestureRecognizer Tapped="List_ItemSelected" Command="{Binding SwitchViewsCommand}"/>
  </Grid.GestureRecognizers>

在ViewModel中,我做到了:

 public ICommand SwitchViewsCommand
    {
        get;
        set;
    }

这在我的构造函数中,并执行我想要的方法。

SwitchViewsCommand = new Command(Tapped);