1
我们有一个自定义ListView
它有一个ItemClickCommand
属性。我们使用local
关键字将它绑定在XAML文件中。它适用于Xamarin.Forms Android,但在Xamarin.forms iOS中不可点击。ListView ItemClick不能在Xamarin.Forms(iOS)中工作
请建议如何继续或我们在代码中做错了什么。
代码写入如下:
XAML文件:
<StackLayout x:Class="XYZ"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XYZ.UI;assembly=XYZ">
<local:ListView ItemClickCommand="{Binding OnDataSelectionCommand, Mode=TwoWay}" ItemsSource="{Binding DataList}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label FontSize="17"
HorizontalOptions="Start"
Text="{Binding }"
TextColor="Black"
VerticalOptions="Center" />
<Label FontSize="17"
HorizontalOptions="Start"
Text=" , "
TextColor="Black"
VerticalOptions="Center" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</local:ListView>
视图模型:
private Command _onDataSelectionCommand;
public Command OnDataSelectionCommand
{
get
{
return _onDataSelectionCommand ?? (_onDataSelectionCommand = new Command(async (s) =>
{
// TODO: get selected item from list !!
}));
}
}
自定义列表创建:
using System.Windows.Input;
using Xamarin.Forms;
namespace XYZ.UI
{
public class ListView : Xamarin.Forms.ListView
{
public static BindableProperty ItemClickCommandProperty = BindableProperty.Create<ListView, ICommand>(x => x.ItemClickCommand, null);
public ListView()
{
this.ItemTapped += this.OnItemTapped;
}
public ICommand ItemClickCommand
{
get { return (ICommand)this.GetValue(ItemClickCommandProperty); }
set { this.SetValue(ItemClickCommandProperty, value); }
}
private void OnItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item != null && this.ItemClickCommand != null && this.ItemClickCommand.CanExecute(e))
{
this.ItemClickCommand.Execute(e.Item);
}
}
}
感谢您的答复,对于IOS点击正在工作,但长时间按 –
长按列表项? –
是的,我们有一个列表视图,上面的代码工作正常,我们可以从ListView中选择项目。但在iOS的情况下,我们需要长时间按下该项目才能启动事件。 –