gpt4 book ai didi

c# - 在 ListView 中的图像上使用点击手势

转载 作者:行者123 更新时间:2023-11-30 20:17:15 27 4
gpt4 key购买 nike

我在 Xamarin 表单应用程序中使用 Prism,我有一个 ListView,其中每个 Listitem 有 2 个图像、一个编辑图像和一个删除图像。

我在这两个图像上使用了TapGesturesRecognizers,并为这些TapGestureRecognizers绑定(bind)了_DelegateCommands。然而,这些 DelegateCommands 不会被调用。

提前致谢

这是我的 XAML 代码

<ListView x:Name="lstAddress" HasUnevenRows="True" SeparatorVisibility="None" ItemsSource="{Binding ListItems}" CachingStrategy="RecycleElement" >
<ListView.Behaviors>
<b:EventToCommandBehavior EventName="ItemTapped"
Command="{Binding ListItemSelectCommand}" EventArgsParameterPath="Item"/>
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Description}" FontSize="15" TextColor="#959595">
</Label>
<Image Source="Edit.png" HeightRequest="50" WidthRequest="50" IsVisible="{Binding ShowEdit}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command ="{Binding EditAddressCommand}"></TapGestureRecognizer>
</Image.GestureRecognizers>
</Image>
<Image Source="Delete.png" ClassId="{Binding ListID}" HeightRequest="30" WidthRequest="30" IsVisible="{Binding ShowIcon}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command ="{Binding DeleteAddressCommand}"></TapGestureRecognizer>
</Image.GestureRecognizers>
</Image>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

在我的 View 模型中:

public class AddressBookViewModel : BindableBase
{
INavigationService _navigationService;
public DelegateCommand EditAddressCommand { get; set; }
public DelegateCommand DeleteAddressCommand { get; set; }

public ObservableCollection<ListModel> ListItems {get;set;} = new ObservableCollection<ListModel>();

public DelegateCommand<ListModel> ListItemSelectCommand { get; set; }


public MyZypListsViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
EditAddressCommand = new DelegateCommand(EditAddress);
DeleteAddressCommand = new DelegateCommand(DeleteAddress);
ListItemSelectCommand = new DelegateCommand<ListModel>(ListItemSelected);
}

private void EditAddress()
{
//Edit listitem
}

private void DeleteAddress()
{
//Delete listitem
}
}

最佳答案

对于列表中的每个项目,您的 BindingContextListViewBindingContext 不同。在本地,您的 BindingContext 是项目本身,这就是您可以获得“Description”属性的原因。 Xamarin 正在您的项目中而不是 View 模型中搜索名为 EditAddressCommandICommand

如果您愿意,您可以创建交叉绑定(bind)引用...只需将项目模板中的命令替换为以下方式:

Command ="{Binding BindingContext.EditAddressCommand, Source={x:Reference lstAddress}" 

它可能会起作用。

关于c# - 在 ListView 中的图像上使用点击手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45485312/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com