如何知道点击了contextAction menuItem的listView viewCell?
How to know the listView viewCell whose contextAction menuItem is clicked?
我在 xaml 中创建了一个 listView(使用 xamarin 在线跨平台开发指南),如下所示:
<ListView x:Name="AccountsList" ItemSelected="AccountSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Clicked="OnEdit"
Text="Edit" />
<MenuItem Clicked="OnDelete"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
<ViewCell.View>
<StackLayout Orientation="Vertical"
HorizontalOptions="StartAndExpand">
<Label Text="{Binding DeviceName}"
HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我像这样以编程方式分配 ItemSource:AccountsList.ItemsSource = App.Database.GetAccounts ();
这是我的 'onDelete' 方法:
public void OnDelete (object sender, EventArgs e) {
var mi = ((MenuItem)sender);
DisplayAlert("Delete Context Action", mi.Command + " delete context action", "OK");
}
我想知道具体是哪个viewCell的menuItem被点击了,即从上面的'onDelete'方法中引用对象的'DeviceName'。我知道我可以用这样的 listView 的 'ItemSelected' 方法来做到这一点:
void AccountSelected(object sender, SelectedItemChangedEventArgs e)
{
var _account = (Account)e.SelectedItem;
// And access the account here.
}
但是 'Clicked' 方法不允许 'void OnDelete (object sender, SelectedItemChangedEventArgs e)' 签名(而且我怀疑它是否会起作用)。
我怎样才能做到这一点?
您可以像下面的例子一样绑定行的记录id,
<MenuItem Clicked="OnDelete" Text="Delete" IsDestructive="True" CommandParameter="{Binding RecordIdentifier}"/>
并且在您的 OnDelete
事件处理程序上,您可以从 CommandParameter
中获取我们之前绑定的记录 ID,类似于下面的示例,
public void OnDelete (object sender, EventArgs e)
{
var mi = ((MenuItem)sender);
DisplayAlert("Delete Context Action", mi.CommandParameter + " delete context action", "OK");
}
我在 xaml 中创建了一个 listView(使用 xamarin 在线跨平台开发指南),如下所示:
<ListView x:Name="AccountsList" ItemSelected="AccountSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Clicked="OnEdit"
Text="Edit" />
<MenuItem Clicked="OnDelete"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
<ViewCell.View>
<StackLayout Orientation="Vertical"
HorizontalOptions="StartAndExpand">
<Label Text="{Binding DeviceName}"
HorizontalOptions="CenterAndExpand"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我像这样以编程方式分配 ItemSource:AccountsList.ItemsSource = App.Database.GetAccounts ();
这是我的 'onDelete' 方法:
public void OnDelete (object sender, EventArgs e) {
var mi = ((MenuItem)sender);
DisplayAlert("Delete Context Action", mi.Command + " delete context action", "OK");
}
我想知道具体是哪个viewCell的menuItem被点击了,即从上面的'onDelete'方法中引用对象的'DeviceName'。我知道我可以用这样的 listView 的 'ItemSelected' 方法来做到这一点:
void AccountSelected(object sender, SelectedItemChangedEventArgs e)
{
var _account = (Account)e.SelectedItem;
// And access the account here.
}
但是 'Clicked' 方法不允许 'void OnDelete (object sender, SelectedItemChangedEventArgs e)' 签名(而且我怀疑它是否会起作用)。
我怎样才能做到这一点?
您可以像下面的例子一样绑定行的记录id,
<MenuItem Clicked="OnDelete" Text="Delete" IsDestructive="True" CommandParameter="{Binding RecordIdentifier}"/>
并且在您的 OnDelete
事件处理程序上,您可以从 CommandParameter
中获取我们之前绑定的记录 ID,类似于下面的示例,
public void OnDelete (object sender, EventArgs e)
{
var mi = ((MenuItem)sender);
DisplayAlert("Delete Context Action", mi.CommandParameter + " delete context action", "OK");
}