2017-08-03 107 views
1

我在我的Xamarin表单应用程序中使用棱镜,我有一个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> 

在我的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 
    } 
} 

回答

3

对不起,我英文不好

这是因为对于列表中的每个项目,Binding上下文与ListView不同。 Localy你的绑定上下文是项目本身,这就是为什么你可以获得'描述'属性,例如。 Xamarin正在您的项目中搜索名为“EditAddressCommand”的命令,而不是在您的视图模型中。

如果你愿意,你可以交叉绑定引用......只需用这种方式替换项目模板中的命令:Command ="{Binding BindingContext.EditAddressCommand, Source={x:Reference lstAddress}",它可能有效。

我希望它能帮助你。