绑定命令根本不响应

时间:2019-04-23 12:49:46

标签: mvvm xamarin.forms binding navigation command

我已经使用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>

因此,我以与TabTappedCommandHideListOnTapCommand相同的方式进行了所有操作,但是很明显我错过了一些东西……

1 个答案:

答案 0 :(得分:0)

在导航方法时,它需要异步。因此,您的方法将如下所示:

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