【问题标题】:Xamarin Forms TapGestureRecognizer Is not issuing commandXamarin Forms TapGestureRecognizer 未发出命令
【发布时间】:2021-08-03 21:07:39
【问题描述】:

所以我有一个 xamarin 表单应用程序,目前仅针对 android 实现。我正在尝试实现一个点击事件。当被点击时,虽然这永远不会命中 ViewModel 中的命令。我不确定我的代码是否有问题,或者我只是执行错误。

ViewModel 代码:

    private RelayCommand<object> _OnClickableLabel;

    public RelayCommand<object> OnClickableLabel
    {
        get { return _OnClickableLabel ?? (_OnClickableLabel = new RelayCommand<object>((currentObject) => Test(currentObject))); }
    }

    private void Test(object currentObject)
    {
        Application.Current.MainPage.DisplayAlert("Alert", "were going down cap", "OK");
    }

页面 Xaml:

  <ListView Grid.Row="1" ItemsSource="{Binding Notifications}" RowHeight="100">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Vertical"  BackgroundColor="{Binding BackgroundColor}">
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding OnClickableLabel }" />
                    </StackLayout.GestureRecognizers>
                    <Label FontSize="Large" Text="{Binding Title}"></Label>
                    <Label FontSize="Small" Text="{Binding Text}"></Label>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我已经使用页面的 cs 代码中的方法对其进行了测试,效果很好,但它必须在 ViewModel 中实现,因为它会影响该数据。

【问题讨论】:

  • 您的命令在基础虚拟机上,但ListView每一行BindingContext 是一个单独的Notification 对象,而不是整个虚拟机。您可以使用relative binding 来解决此问题
  • 不知道,对 Xamarin 来说很新。我假设我使用的是 AncestorType 版本的 relativeSource?我在哪里可以确定 AncestorType 应该是什么类型?将 ViewModel 放在那里似乎不起作用。
  • 另一种解决方法可能是为您的页面命名,例如x:Name="this",然后在绑定上设置源,例如:Command="{Binding OnClickableLabel, Source={x:Reference this} }"

标签: c# xamarin xamarin.forms mvvm xamarin.android


【解决方案1】:

根据您的描述,您想在 ListView 中 add a tap gesture recognizer,并希望将 ListView 当前行数据传递给 TapGestureRecognizer 事件,对吗?

如果是,按照Jason的意见,你需要先看看Xamarin.Forms Relative Bindings,将ListView命名为listview1,然后看看下面的代码:

<ListView
            x:Name="listview1"
            Grid.Row="1"
            ItemsSource="{Binding Notifications}"
            RowHeight="100">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="{Binding BackgroundColor}" Orientation="Vertical">
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer Command="{Binding BindingContext.OnClickableLabel, Source={x:Reference listview1}}" CommandParameter="{Binding .}" />
                            </StackLayout.GestureRecognizers>
                            <Label FontSize="Large" Text="{Binding Title}" />
                            <Label FontSize="Small" Text="{Binding Text}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

public partial class Page16 : ContentPage
{
    public ObservableCollection<Notclass> Notifications { get; set; }
    public ICommand OnClickableLabel { get; set; }
    public Page16()
    {
        InitializeComponent();
        Notifications = new ObservableCollection<Notclass>()
        {
            new Notclass(){Title="title 1",Text="notification 1"},
            new Notclass(){Title="title 2",Text="notification 2"},
            new Notclass(){Title="title 3",Text="notification 3"},
            new Notclass(){Title="title 4",Text="notification 4"},
            new Notclass(){Title="title 5",Text="notification 5"}

        };
        OnClickableLabel = new Command(n=> {
            var vm = (Notclass)n;
            Application.Current.MainPage.DisplayAlert("Alert",vm.Title , "OK"); 
        });

        this.BindingContext = this;
    }
}

public class Notclass
{
    public string Title { get; set; }
    public string Text { get; set; }
    public Color BackgroundColor { get; set; } = Color.White;
}

【讨论】:

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