【问题标题】:Xamarin ICommand not firingXamarin ICommand 未触发
【发布时间】:2017-07-08 04:47:53
【问题描述】:

我是 MVVM 的新手,我在 Xamarin.Forms 应用程序中出现了一些问题。

这是我在 SessionsPage.xml 中的内容

<Grid.Children>
    <Image Source="zero.jpg" Grid.Row="0" Grid.Column="0">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
            Command="{Binding TapCommand}"
            CommandParameter="0" />
     </Image.GestureRecognizers>

我有一个链接到命令“TapCommand”的图像。

在这个视图的构造函数中,我添加了:

tapViewModel = new EasyScrum.Views.TapViewModel();

ViewModel 类是:

public class TapViewModel : INotifyPropertyChanged
{
    int taps = 0;
    ICommand tapCommand;

    public event PropertyChangedEventHandler PropertyChanged;

    public TapViewModel()
    {
        // configure the TapCommand with a method
        tapCommand = new Command(OnTapped);
    }

    public ICommand TapCommand
    {
        get { return tapCommand; }
    }
    void OnTapped(object s)
    {
        taps++;
        Debug.WriteLine("parameter: " + s);
    }
    //region INotifyPropertyChanged code omitted
}

事件没有触发,怎么回事?

【问题讨论】:

    标签: xaml xamarin mvvm xamarin.forms


    【解决方案1】:

    我自己回答。

    我的错误是我必须分配

    this.BindingContext = new TapViewModel();

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 2016-07-30
      • 2018-03-10
      • 2021-09-18
      相关资源
      最近更新 更多