简体   繁体   English

Xamarin Forms 在图像单击时获取对象元素

[英]Xamarin Forms Get Object element on image click

I have a problem.我有个问题。 I created a CollectionView from a List called: List<Set> Sets and in a Set I have a List called List<Picture> Pictures .我从一个名为List<Set> Sets的 List 创建了一个 CollectionView ,在一个 Set 中我有一个名为List<Picture> Pictures

Here is what the classes look like:下面是这些类的样子:

public class Set
{
    public int Id { get; set; }
    public List<Picture> Pictures{ get; set; }
}

And the picture class looks like this:图片类看起来像这样:

public class Picture
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Now in my XAML I added a click event on every image, because I need to know which image has been clicked.现在在我的 XAML 中,我在每个图像上添加了一个点击事件,因为我需要知道哪个图像被点击了。 The problem is that I also need to know in which set the image click has been done, because an image can be in multiple sets.问题是我还需要知道在哪一组中单击了图像,因为图像可以在多个组中。

Here is the XAML:这是 XAML:

<CollectionView ItemsSource="{Binding Sets}">
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Vertical" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <ScrollView Orientation="Horizontal">
                <StackLayout BindableLayout.ItemsSource="{Binding Pictures}" Orientation="Horizontal">
                    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <ff:CachedImage Aspect="AspectFill" Source="{Binding imageSource}">
                                <ff:CachedImage.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="AlbumFoto_Clicked" />
                                </ff:CachedImage.GestureRecognizers>
                            </ff:CachedImage>
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </StackLayout>
            </ScrollView>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

Now I already got this code:现在我已经得到了这个代码:

private void AlbumFoto_Clicked(object sender, EventArgs e)
{
    CachedImage image = (CachedImage)sender;
    var setPicture = image.Source.BindingContext as Picture;
    var ImageId = setPicture.Id;

    var Pictureindex = 0;
    foreach (var set in Sets.Where(set => Pictures.Picture.Any(p => p.Id == ImageId)))
    {
        Pictureindex = set.Pictures.Picture.FindIndex(p => p.Id == ImageId);
    }
}

Now how can I get the Id from the set of clicked image?现在如何从单击的图像集中获取 Id?

Instead of using an click event, you should use Command and CommandParameter and use the BindingContext of the parent Collection您应该使用CommandCommandParameter并使用父集合的 BindingContext,而不是使用单击事件

<CollectionView ItemsSource="{Binding Sets}">
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Vertical" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <ScrollView x:Name="ParentCollection" Orientation="Horizontal">
                <StackLayout BindableLayout.ItemsSource="{Binding Pictures}" Orientation="Horizontal">
                    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <ff:CachedImage Aspect="AspectFill" Source="{Binding imageSource}">
                                <ff:CachedImage.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding ImageClickCommand}" CommandParameter="{Binding BindingContext.Id, Source={x:Reference Name=ParentCollection}}"/>
                                </ff:CachedImage.GestureRecognizers>
                             </ff:CachedImage>
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </StackLayout>
            </ScrollView>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

And in your ViewModel:在您的 ViewModel 中:

public Command<int> ImageClickCommand{ get; set; }

And in your ViewModel Constructor:在您的 ViewModel 构造函数中:

ImageClickCommand = new Command<int>((Id) =>
{
    //Id -> id of the image
});

But if you still want to use the Clicked Event, i would sugest you do this change.但是,如果您仍然想使用 Clicked 事件,我建议您进行此更改。

Add a new property to your Picture Class:向您的图片类添加一个新属性:

public class Picture
{
    public int Id { get; set; }
    public int SetId { get; set; }
    public string Name { get; set; }
}

Assuming that you receive that Collection from a Service, you could iterate over it and set your SetId like this:假设您从服务收到该集合,您可以迭代它并像这样设置您的 SetId:

foreach (var item in Collection)
{
    foreach (var picture in item.Pictures)
    {
         picture.SetId = item.Id   
    }         
}

Now in your Clicked you should have access to both Picture Id and the respetive Set Id:现在在您的 Clicked 中,您应该可以访问图片 ID 和相应的 Set Id:

private void AlbumFoto_Clicked(object sender, EventArgs e)
{
    CachedImage image = (CachedImage)sender;
    var setPicture = image.Source.BindingContext as Picture;
    var ImageId = setPicture.Id;
    var SetId = setPicture.SetId;
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM