【问题标题】:Binding Command doesn't respond at all绑定命令根本没有响应
【发布时间】:2019-04-23 12:49:45
【问题描述】:

我已经使用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 相同的方式完成了所有操作,但显然我遗漏了一些东西......

【问题讨论】:

  • 你的布局真的没有手势识别器以外的内容吗?
  • 为了清楚起见,您是否在HideListOnTap 中设置了断点并且没有命中?还是您只是没有看到任何与您预期的功能不同的错误?
  • @Jason 当然他们有内容。我刚刚发布了示例代码部分,其中我将 tapGestureRecognizer 绑定到命令等导入的内容。 (我没有发布定义边距、平移、文本等的部分代码......:D)
  • @Knoop HideListOnTap 是工作正常的例子......我的问题是ShowDetailPage。 :D 我在ShowDetailPage 中设置了断点,它没有被命中...
  • TabTappedCommand 有一个参数,所以它应该被定义为一个 ICommand

标签: mvvm xamarin.forms binding navigation command


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多