我的ViewModel中有这个:

public class MyClass: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    int taps = 0;
    ICommand tapCommand;

    public MyClass()
    {
        tapCommand = new Command(OnTapped);
    }

    public ICommand TapCommand
    {
        get { return tapCommand; }
    }

    void OnTapped(object s)
    {
        taps++;
        Debug.WriteLine("parameter: " + s);
    }
}


而在xaml中:

<Image Source="delete.jpg" HeightRequest="20" WidthRequest="20">
     <Image.GestureRecognizers>
        <TapGestureRecognizer
            Command="{Binding TapCommand}"
            CommandParameter="Image1" />
    </Image.GestureRecognizers>
 </Image>


但是单击该图像时,输出日志中将不显示任何内容。我想念什么?


  注意1:我已按照指南here
  
  注意2:我正在Android设备上调试
  
  更新1:完整的xaml here

最佳答案

您没有在后面提供任何代码,因此我必须创建自己的代码(请参见下文)。
如果注释掉ListView IsEnabled =“ False”,您还将为我节省15分钟

设置Command="{Binding TapCommand}时,TapCommand对应于列表项的TapCommand,而不是模型本身。因此,您有2个选择


您可以在MyItem中实现click(我认为您不需要这样做,因此下面在MyItem类中对此的部分代码进行了注释)
在图像本身上定义上下文绑定。
因此,我们的视图模型可以创建几次-我们不希望这样,所以最好的解决方案是定义视图模型的静态资源并在页面上的任何地方使用它。
解决方案如下(您只需要修改一些名称空间并将delte.png更改为delete.jpg):


页面xml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ButtonRendererDemo;assembly=ButtonRendererDemo"
             x:Class="ButtonRendererDemo.ImageTapComplexPage"
             BindingContext="{StaticResource viewModel}">

      <ContentPage.Resources>
        <ResourceDictionary>
          <local:TapComplexViewModel x:Key="viewModel"/>
        </ResourceDictionary>
      </ContentPage.Resources>


 <StackLayout>
    <Label Text="text2" VerticalOptions="Center" HorizontalOptions="Center" />
    <StackLayout Padding="0,20,0,20">
    <Button Text="text" Clicked="onclick" BackgroundColor="#009688"></Button>

    </StackLayout>
    <Label Text="List Type" VerticalOptions="Center" HorizontalOptions="Center" />
    <ListView x:Name="lstItems" RowHeight="60" ItemsSource="{Binding Items}" > <!--IsEnabled="False"-->
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,2,0,2">
              <StackLayout Padding="5,5,5,5">
                <Image Source="{Binding source}" HorizontalOptions="End" HeightRequest="40" WidthRequest="40" />
              </StackLayout>
              <StackLayout Orientation="Vertical" Spacing="1">
                <Label Text = "{Binding name}" HeightRequest="20" FontAttributes="Bold"/>
                <Label Text = "{Binding data, StringFormat='{0:F0}'}" />
              </StackLayout>
              <StackLayout HorizontalOptions="EndAndExpand" Padding="0,0,15,0" Orientation="Horizontal">
                <Image Source="delete.png" HeightRequest="20" WidthRequest="20">
                   <Image.GestureRecognizers>
                    <!--<TapGestureRecognizer
                        Command="{Binding TapCommand}"
                        CommandParameter="Image1" />-->
                        <TapGestureRecognizer Command="{Binding Source={StaticResource viewModel}, Path=TapCommand}" CommandParameter="{Binding name}" />
                 </Image.GestureRecognizers>
                </Image>
              </StackLayout>
            </StackLayout>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </StackLayout>
</ContentPage>


后面的代码

namespace ButtonRendererDemo
{
    public partial class ImageTapComplexPage : ContentPage
    {
        public ImageTapComplexPage()
        {
            InitializeComponent();
        }

        void onclick(object sender, EventArgs args)
        {

        }
    }

    public class TapComplexViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        int taps = 0;
        ICommand tapCommand;

        public ObservableCollection<MyItem> Items { get; private set; }
        public TapComplexViewModel()
        {
            Items = new ObservableCollection<MyItem>()
            {
                new MyItem { name="First", source="Icon.png", data=0.5f },
                new MyItem { name="Second", source="Icon.png", data=0.6f },
                new MyItem { name="Third", source="Icon.png", data=0.7f }
            };

            tapCommand = new Command(OnTapped);
        }

        public ICommand TapCommand
        {
            get { return tapCommand; }
        }

        void OnTapped(object s)
        {
            taps++;
            Debug.WriteLine("parameter: " + s);
        }



    }

    public class MyItem
    {
        public string name { get; set; }
        public string source { get; set; }
        public float data { get; set; }

        //public ICommand TapCommand
        //{
        //    get { return new Command(() => { }); }
        //}
    }
}


android - ListView中的点按手势手势识别器不起作用-LMLPHP

08-26 04:08