c# - 如何从 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 获得相同的结果呢?
谢谢,
解决方案
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));
}
}
推荐阅读
- powershell - 具有某些属性的最小值/最大值的自定义对象数组元素
- html - html twig 文件在 Symfony 应用程序中找不到我的 css 文件的路径
- docker - 暴露多个本地运行的 docker 容器
- blockchain - 超级账本自动化私有网络部署(Besu BAAS - 区块链即服务)
- angular - Angular 会检查静态绑定的差异吗?
- excel - 关于没有错误的错误 VBA 行活动
- python - 查找不等于 X 的 EC2 实例 - AWS CLI
- spring - ActiveDirectoryLdapAuthenticationProvider.loadUserAuthorities 行为
- swift - 如何注册一个可变参数函数以在swift中接收js
- python - 将一列df中的字符串值检查到具有多个条件的另一列df