在列表视图中的图像上使用拍打手势

Nat*_*n N 2 c# listview prism mvvm xamarin.forms

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

我已经TapGesturesRecognizers在这2张图片上使用了,并DelegateCommands为它们绑定了_ TapGestureRecognizers。但是,这些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>
Run Code Online (Sandbox Code Playgroud)

在我的ViewModel中:

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   
    }
}
Run Code Online (Sandbox Code Playgroud)

Die*_*uza 6

对于名单上的每一个项目,你BindingContext是不一样ListViewBindingContext。在本地,您BindingContext就是项目本身,例如,这就是为什么您可以获取'Description'属性的原因。在您的商品而不是视图模型中Xamarin搜索ICommand被叫EditAddressCommand

您可以根据需要创建交叉绑定引用。只需替换项目模板上的命令即可:

Command ="{Binding BindingContext.EditAddressCommand, Source={x:Reference lstAddress}" 
Run Code Online (Sandbox Code Playgroud)

它可能会起作用。