首页 > 解决方案 > 绑定命令根本没有响应

问题描述

我已经使用TapGestureRecognizer并将它们绑定到某个命令并且该命令工作正常......

这是示例:

IrrigNetPage.xaml(查看)

<StackLayout.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding TabTappedCommand}" CommandParameter="map"/>
</StackLayout.GestureRecognizers>

<Grid IsVisible="{Binding IsGridHeaderVisible}">
    <Grid.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding HideListOnTapCommand}"/>
    </Grid.GestureRecognizers>
</Grid>

IrrigNetPage.xaml.cs

public partial class IrrigNetPage : ContentPage
{
    public IrrigNetPage ()
    {
        InitializeComponent ();
        BindingContext = new IrrigNetViewModel();
    }
}

IrrigNetViewModel.cs

[AddINotifyPropertyChangedInterface]
public class IrrigNetViewModel : PopupPage
{
    public ICommand TabTappedCommand { get; }
    public ICommand HideListOnTapCommand { get; }
    public ICommand ShowIrrigNetDetailPageCommand { get; }

    public IrrigNetViewModel()
    {
        TabTappedCommand = new Command((tabName) => OnTapClicked(tabName.ToString()));
        HideListOnTapCommand = new Command(HideListOnTap);
        ShowIrrigNetDetailPageCommand = new Command(ShowDetailPage);

    private void ShowDetailPage()
    {
        Navigation.PushPopupAsync(new IrrigNetDetailsPage());
    }

    private void HideListOnTap()
    {
        IsListVisible = !IsListVisible;
    }

    private void OnTapClicked(string tabName)
    {

        if (tabName == "location")
        {
....

所以我对 TabTappedCommand 和 HideListOnTapCommand 以与 ShowIrrigNetDetailPageCommand 相同的方式完成了一切,但由于某种原因,当我点击 TapGestureRecognizer 时什么也没有发生。我试图调试,但我没有得到任何异常或错误......只是没有发生任何事情......

我安装了 Rg.Plugins.Popup,因为 IrrigNetDetailsPage.xaml 是<pages:PopupPage mlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"> ...

IrrigNetDetailsPage.xaml.cs

public partial class IrrigNetDetailsPage : PopupPage
{
    public IrrigNetDetailsPage ()
    {
        InitializeComponent ();
        BindingContext = new IrrigNetDetailsViewModel();
    }
}

IrrigNetPage.xaml 绑定(不起作用)

<Frame.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding ShowIrrigNetDetailPageCommand}"/>
</Frame.GestureRecognizers>

TabTappedCommand因此,我以与 and相同的方式完成了所有操作HideListOnTapCommand,但显然我遗漏了一些东西...

标签: mvvmxamarin.formsbindingnavigationcommand

解决方案


当你在一个方法中导航时,它需要是异步的。所以你的方法看起来像这样:

private async void ShowDetailPage()
    {
        Navigation.PushPopupAsync(new IrrigNetDetailsPage());
    }

推荐阅读