【问题标题】:Xamarin Forms stacklayout TapGestureRecognizer command not calledXamarin Forms stacklayout TapGestureRecognizer 命令未调用
【发布时间】:2019-07-30 04:57:02
【问题描述】:

我已经使用 TapGestureRecognizer 在 StackLayout 上添加了一个命令,但它没有调用 viewmodel 类。

这是 xaml 代码:

<StackLayout Padding="10" Spacing="0">
    <Image Source="edit_black" WidthRequest="20" />
    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding EditServiceCommand}" CommandParameter="{Binding .}" />
    </StackLayout.GestureRecognizers>
</StackLayout>

ViewModel 类命令方法:

public ICommand EditServiceCommand
        {
            get => new Command((item) => { _popupNavigation.PushAsync(new AddServicePopup("edit"), true); });
        }

【问题讨论】:

  • 您是否已经将页面绑定上下文分配给您的视图模型?
  • 是的,我用的是棱镜
  • 您是如何在 prism 中注册视图模型的?你的视图模型的构造函数被调用了吗?
  • Viewmodel 正在工作,我可以从 viewmodel 中获取列表。
  • 如果您使用的是 Prism,您可能需要使用 DelegateCommand

标签: xamarin mvvm xamarin.forms navigation


【解决方案1】:

请将您的视图模型命令代码更新为以下

 private ICommand _EditServiceCommand;
 public ICommand EditServiceCommand => _EditServiceCommand ?? (_EditServiceCommand = new Command((item) =>
    {
        _popupNavigation.PushAsync(new AddServicePopup("edit"), true);
    }));

【讨论】:

  • 为我工作。可能还有其他问题。请尝试分享更多代码
【解决方案2】:

你需要告诉命令item是什么类型。

public ICommand EditServiceCommand
{
     get => new Command<CommandParameterType>((item) => { _popupNavigation.PushAsync(new AddServicePopup("edit"), true); });
}

但在您的代码中,您没有使用 item,因此您可以在没有 CommandParameter 的情况下调用命令。

get => new Command(() => _popupNavigation.PushAsync(new AddServicePopup("edit"), true));

【讨论】:

    猜你喜欢
    • 2017-11-12
    • 2021-08-03
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2017-12-09
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多