简体   繁体   中英

Xamarin.Forms: Binding in style dont work

Visit Xamarin.Forms : How to set 'GestureRecognizers' in style

Hello,

I dont understand this line of code in the related link.

<Setter Property="MyCommand" Value="{Binding TapCommand}"/>

There is no property "TapCommand" to bind with. What should I write into value?

Thank you :)

You will need to define it in your related view model. like public ICommand TapCommand{get;} and then bind it in style. More details you can check at https://blog.xamarin.com/simplifying-events-with-commanding/

The TapCommand is defined in a view model class.

For example:

public class TapViewModel : INotifyPropertyChanged
{
    int taps = 0;
    ICommand tapCommand;
    public TapViewModel () {
        // configure the TapCommand with a method
        tapCommand = new Command (OnTapped);
    }
    public ICommand TapCommand {
        get { return tapCommand; }
    }
    void OnTapped (object s)  {
        taps++;
        //perform your actions here
    }

    //region INotifyPropertyChanged code omitted
}

You can define your own action in the OnTapped function. Once you {Binding TapCommand} , OnTapped will be triggered when you tap on the label.

You can refer:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/commanding

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