gpt4 book ai didi

android - 在 ListView 中点击 Gesture 手势识别器不工作

转载 作者:行者123 更新时间:2023-11-29 17:10:32 25 4
gpt4 key购买 nike

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

但是当单击图像时,输出日志中没有任何内容。我缺少什么?

Note 1: I've followed the guide here

Note 2: I'm debugging on Android Device

Update 1: full xaml here

最佳答案

你没有提供任何背后的代码,所以我不得不创建我自己的(见下文)。如果注释掉 ListView IsEnabled="False"

,您还可以为我节省 15 分钟

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

  1. 您可以在 MyItem 中实现点击(我不认为您想要那样,所以它的部分代码在下面的 MyItem 类中进行了注释)
  2. 定义图像本身的上下文绑定(bind)。因为我们的 View 模型可以被创建几次——我们不希望这样,所以最好的解决方案是定义 View 模型的静态资源并在页面的任何地方使用它。解决方案如下(您只需要修改一些命名空间并将 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(() => { }); }
//}
}
}

enter image description here

关于android - 在 ListView 中点击 Gesture 手势识别器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40660528/

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