Xamarin fors stacklayout tapgesturerecognizer命令未调用



我使用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); });
        }

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

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

您需要告诉命令哪种类型item是。

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

但是在您的代码中,您不使用项目,因此您可以在没有命令参数的情况下调用命令。

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

相关内容

  • 没有找到相关文章

最新更新