如何在Xamarin表单的集合视图上添加Click事件



我的Xamarin Forms项目中有一个图像的集合视图。我想在图像上添加onClick事件,以导航到每个图像的相应页面。我试图找到解决方案,但没有确切的答案

您可以在Image上添加TapGestureRecognizer

在xaml中

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
x:Class="xxx"

x:Name="CurrentPage" //set name here  >
<CollectionView ItemsSource="{Binding Monkeys}"
ItemsLayout="HorizontalList">
<CollectionView.ItemTemplate>
<DataTemplate>                   
<Image  
Source="{Binding xxx}" 
Aspect="AspectFill"
HeightRequest="xxx" 
WidthRequest="xxx" >
<Image.GestureRecognizers>
<TapGestureRecognizer  Command="{Binding Source={x:Reference CurrentPage}, Path=BindingContext.ClickCommand}" />
</Image.GestureRecognizers>
</Image>

</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

在ViewModel中

public ICommand ClickCommand { get; private set; }
INavigation Navigation;
public xxxViewModel(INavigation navigation)
{
Navigation = navigation;
ClickCommand = new Command(() => {
// navigation.PushAsync(xxx);
});
}

在ContentPage中

public xxxPage()
{
InitializeComponent();
BindingContext = new xxxViewModel(this.Navigation);
}

最新更新