首页 > 解决方案 > 如何从 ContentView 上下文中使用的 ListView 绑定“轻拍”事件处理程序?

问题描述

在我的 Xamarin 项目中。由于“ContentView”,我想创建一个使用 ListView 的控件。我正在使用 ContentView 方法,因为我应该在我的项目中以模块化方式多次重复使用此控件元素。我可以成功地为我的视图的不同元素创建绑定属性。但是我找不到为与我的 Listview 相关的操作(例如“Tapped”)创建绑定属性的方法。我已经通过使用元素和 EventHandlers 的“命令”属性(例如 Button.Command)在其他 ContentView 中进行了此操作。但听起来 ListView 没有实现这个属性。

这是我迄今为止为我的 ListView 使用的代码

    public partial class ItemSelectListView : ContentView
{
    public static readonly BindableProperty ItemListViewProperty=
        BindableProperty.Create("ItemListView", typeof(IEnumerable<Item>), typeof(ItemSelectListView), default(Item));

    public IEnumerable<Item> ItemListView
    {
        get { return (IEnumerable<Item>)GetValue(ItemListViewProperty); }
        set { SetValue(ItemListViewProperty, value); }
    }


    public static readonly BindableProperty TitleLabelProperty =
        BindableProperty.Create("TitleLabel", typeof(string), typeof(ItemSelectListView), default(string));

    public string TitleLabel
    {
        get { return (string)GetValue(TitleLabelProperty); }
        set { SetValue(TitleLabelProperty, value); }
    }

    public ItemSelectListView ()
    {
        InitializeComponent ();
        ListView.SetBinding(ListView.ItemsSourceProperty, new Binding("ItemListView", source: this));
        Title.SetBinding(Label.TextProperty, new Binding("TitleLabel", source: this));            

    }
}

例如,这是我用来创建无法为 ListView 复制的 EventHandler 的方式

    public partial class ItemSelectButtonView : ContentView
{
    public static readonly BindableProperty LeftButtonTextProperty =
        BindableProperty.Create("LeftButtonText", typeof(string), typeof(ItemSelectButtonView), default(string));

    public string LeftButtonText
    {
        get { return (string)GetValue(LeftButtonTextProperty);}
        set { SetValue(LeftButtonTextProperty, value); }
    }

    public static readonly BindableProperty RightButtonTextProperty =
        BindableProperty.Create("RightButtonText", typeof(string), typeof(ItemSelectButtonView), default(string));

    public string RightButtonText
    {
        get { return (string)GetValue(RightButtonTextProperty); }
        set { SetValue(RightButtonTextProperty, value); }
    }

    public event EventHandler RightButtonClicked;
    public event EventHandler LeftButtonClicked;



    public ItemSelectButtonView ()
    {
        InitializeComponent ();
        LeftButton.SetBinding(Button.TextProperty, new Binding("LeftButtonText", source: this));
        RightButton.SetBinding(Button.TextProperty, new Binding("RightButtonText", source: this));

        LeftButton.Command = new Command(() =>
        {
            LeftButtonClicked?.Invoke(this, EventArgs.Empty);
        });

        RightButton.Command = new Command(() =>
        {
            RightButtonClicked?.Invoke(this, EventArgs.Empty);
        });
    }

}

那么如何使用 ListView 获得相同的结果呢?

谢谢,

标签: c#xamarin

解决方案


I could finally do the following way. Not sure if it is the cleanest one.

public partial class ItemSelectListView : ContentView
{
    public static readonly BindableProperty ItemListViewProperty=
        BindableProperty.Create("ItemListView", typeof(IEnumerable<Item>), typeof(ItemSelectListView), default(Item));

    public IEnumerable<Item> ItemListView
    {
        get { return (IEnumerable<Item>)GetValue(ItemListViewProperty); }
        set { SetValue(ItemListViewProperty, value); }
    }


    public static readonly BindableProperty TitleLabelProperty =
        BindableProperty.Create("TitleLabel", typeof(string), typeof(ItemSelectListView), default(string));

    public string TitleLabel
    {
        get { return (string)GetValue(TitleLabelProperty); }
        set { SetValue(TitleLabelProperty, value); }
    }

    public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;

    public void NotifyItemSelected(object sender, SelectedItemChangedEventArgs e)
    {            
        ItemSelected(sender, e);
    }

        public ItemSelectListView ()
    {
        InitializeComponent ();
        ItemsListView.SetBinding(ListView.ItemsSourceProperty, new Binding("ItemListView", source: this));
        Title.SetBinding(Label.TextProperty, new Binding("TitleLabel", source: this));
        ItemsListView.ItemSelected += NotifyItemSelected;
        ItemsListView.SetBinding(ListView.SelectedItemProperty, new Binding("ItemSelected", source: this));
    }
}

推荐阅读