简体   繁体   中英

Binding a method to ViewCell Tapped

Is it possible to bind a method to viewcell tapped?

I have the following code:

<ViewCell Tapped="{Binding SwitchViews}">

Which throws the following error:

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

so Instead of giving the tapped event to viewcell give it to the child element of viewcell.

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

for more you can refer my blog post about interactive Listview at https://adityadeshpandeadi.wordpress.com/2018/07/15/the-more-interactive-listview/

feel free to drop by

You probably don't have a function called SwitchViews in your view mode (it may be a property but that's wrong).

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

A common example is:

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

Toggled is the name of an event. The property that was probably intended is called IsToggled.

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

Okay, I got it to work.

I created a Grid inside my ViewCell and added the following code:

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

And in my ViewModel I did this:

 public ICommand SwitchViewsCommand
    {
        get;
        set;
    }

This is in my constructor and executes the method I want.

SwitchViewsCommand = new Command(Tapped);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM