Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to set binding to view model itself?

WinterCloudWinterCloud GBMember ✭✭✭
edited October 2016 in Xamarin.Forms

It's mixed views in a Frame, the Frame's command binding suppose to use view model itself as the parameter.

However, following code gave me error of "Exception has been thrown by the target of an invocation" at runtime.

var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.SetBinding<ViewModel>( TapGestureRecognizer.CommandProperty, x => x.TapCommand ); tapGestureRecognizer.SetBinding<ViewModel>( TapGestureRecognizer.CommandParameterProperty, x => x ); // This line causes error when page renders. GestureRecognizers.Add( tapGestureRecognizer );

Thanks in advance.

Answers

  • WinterCloudWinterCloud GBMember ✭✭✭

    How to do this with expression instead of magic string?

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    If you need to use the viewmodel as the parameter, then remove 'tapGestureRecognizer.SetBinding( TapGestureRecognizer.CommandParameterProperty, x => x );'. Inside the viewmodel, you should have access to it with this.

  • WinterCloudWinterCloud GBMember ✭✭✭

    @AdrianKnight said:
    If you need to use the viewmodel as the parameter, then remove 'tapGestureRecognizer.SetBinding( TapGestureRecognizer.CommandParameterProperty, x => x );'. Inside the viewmodel, you should have access to it with this.

    Thanks for the reply.

    However, I don't have a Command for each view model. I have a few hundreds view models, and one command. That's why I need to use the parameter.

Sign In or Register to comment.