在ListView中点击手势手势识别器无法正常工作

时间:2016-11-17 16:46:09

标签: android objective-c listview xamarin uitapgesturerecognizer

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

3 个答案:

答案 0 :(得分:2)

你没有提供任何代码,所以我必须创建自己的代码(见下文)。 如果注释掉ListView IsEnabled =&#34; False&#34;

,你也可以节省我15分钟

设置Command="{Binding TapCommand}时,TapCommand对应于列表项的TapCommand,而不是模型本身。所以,你有两个选择

  1. 你可以在MyItem中实现点击(我不认为你想要那个,所以它的部分代码在下面的MyItem类中评论过)
  2. 定义图像本身的上下文绑定。 因此我们的视图模型可以创建几次 - 我们不希望这样,所以最好的解决方案是定义视图模型的静态资源并在页面的任何地方使用它。 解决方案如下(您只需要修改一些名称空间并将delte.png更改为delete.jpg):
  3. 第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(() => { }); }
            //}
        }
    }
    

    enter image description here

答案 1 :(得分:1)

在xaml文件的后端,请确保将BindingContext设置为视图模型:如下所示:

xaml.cs

public partial class TapInsideFrameXaml : ContentPage
    {   
        public TapInsideFrameXaml ()
        {
            InitializeComponent ();

            // The TapViewModel contains the TapCommand which is wired up in Xaml
            BindingContext = new TapViewModel ();
        }
}

同时检查:

在菜单上&gt;工具&gt;选项&gt;调试&gt;一般:

  • 确保未选中“将所有输出窗口文本重定向到即时窗口”

关于项目属性&gt;建立:

  • 配置:调试
  • “定义DEBUG常量”已选中
  • 检查“定义TRACE常量”

在“输出”窗口中:

  • 显示输出:Debug
  • 在输出窗口中单击鼠标右键,确保选中“程序输出”

还要确保使用F5而不是Ctrl f5

进行调试

答案 2 :(得分:0)

您声明了PropertyChangedEvent事件,但您实际上从未使用它。试试这个:

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);
        OnPropertyChanged();
    }


    protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler (this, new PropertyChangedEventArgs (propertyName));
    }
}