我有一个派生自 Layout<view>
的类,它是Creating a Custom Layout的WrapLayout示例:
代码有点大,所以我把它放在这个pastebin中:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace CustomLayout.ViewModels
{
// ...
// [EDIT] puting the relevant code for put the images whit
/// a tap gesture in the layout
var images = GetImages();
int i = 0;
foreach (var photo in images.Photos)
{
var image = new Image
{
Source = ImageSource.FromResource(photo),
WidthRequest = 50,
HeightRequest = 75
};
var cartaoTap = new TapGestureRecognizer();
cartaoTap.Tapped += (s, e) =>
{
cartaoClick(i);
};
image.GestureRecognizers.Add(cartaoTap);
customLayout.Children.Add(image);
i++;
}
// ...
private void cartaoClick(int index){
Navigation.PushAsync(new CartaoPage(cartao.GetImagem(index), cartao.GetNome(index), cartao.GetDescricao(index)));
}
}
pastebin Raw Code for the CustomLayout class
“Cartao”是一个具有Nome,Descricao和Imagem等属性的类 . 在tap手势事件中,当我在CustomLayout中添加图像时,我将int“i”值传递给CartaoClcik(int index)方法,在我看来,当我添加时,我可以将索引传递给此方法的调用单击鼠标中的图像显示其中一个图像,但是,当我运行并单击图像时,索引参数是最后添加的图像的alawys
[编辑:] .
在互联网上的一个不人道的搜索工作后,我看到可以实现一个派生自 Image()
的类,然后直接在这个类中实现GestureRecognizer命令,但我不知道如何实现它,我也不知道如何通过页面在“CartaoPage”(图片img,字符串nome,字符串descr)中以全屏显示图像所需的参数,当用户点击图像显示在屏幕中时,参数必须传入
有人可以帮帮我吗?
1 回答
[解决了]
我解决了这个问题,解决了我用
public int ID {get; set;}
创建了一个派生自Image()
的类,然后在使用我自己的类实例化我的图像时填充这个属性,然后我将这个ID传递给我的索引 . 我知道这不是一个优雅的解决方案,但在这里工作 .谢谢大家