关于c#:将方法绑定到已点击的ViewCell

Binding a method to ViewCell Tapped

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

我有以下代码:

1
<ViewCell Tapped="{Binding SwitchViews}">

引发以下错误:

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


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

1
2
3
4
5
6
7
8
9
<Viewcell>
       <Grid>
            <Grid.GestureRecognizers>
                <TapGestureRecognizer Tapped="OpenCaseDetails" />
            </Grid.GestureRecognizers>
            ...
            ...
       </Grid>
</Viewcell>

有关更多信息,请参阅https://adityadeshpandeadi.wordpress.com/2018/07/15/the-more-interactive-listview/上有关交互式Listview的博客文章

随意去


好吧,我知道了。

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

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

在我的ViewModel中,我这样做:

1
2
3
4
5
 public ICommand SwitchViewsCommand
    {
        get;
        set;
    }

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

1
SwitchViewsCommand = new Command(Tapped);

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

This is nearly always caused by assigning a value to an event in XAML, instead of specifying a method name.

A common example is:

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

Toggled is the name of an event. The property that was probably intended is called 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 /)