1
ListView
のカスタムはItemClickCommand
です。 local
キーワードを使用してXAMLファイルにバインドしています。 Android Xamarin.Formsではうまくいきますが、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の場合は、アイテムを長押ししてイベントを発生させる必要があります。 –