如何在C# xamarin窗体中获取活动图像的源代码?

uxhixvfz  于 2022-12-07  发布在  C#
关注(0)|答案(2)|浏览(124)

当用户触摸ImageButton时,我试图从我的***CarouselView***中删除图像。但我不知道如何在我的CarouselView中获取活动图像。
有人知道我怎么能这么做吗?
非常感谢!
代码页.xaml:

<StackLayout Padding="5">
    <CarouselView x:Name="carousel" HeightRequest="600" IndicatorView="{x:Reference contImgs}">
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <Image x:Name="activeImg" Source="{Binding Source}" Aspect="AspectFill"></Image>
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>
    <IndicatorView x:Name="contImgs" IndicatorColor="#bbb" SelectedIndicatorColor="#000" IndicatorSize="12" IndicatorsShape="Circle"></IndicatorView>
    <ImageButton Source="drawable/icon_trash.png" WidthRequest="100" Clicked="btnDeleteImg" HeightRequest="100" AbsoluteLayout.LayoutBounds="0.98,0.07,50,50" AbsoluteLayout.LayoutFlags="PositionProportional"></ImageButton>
</StackLayout>

代码页. xaml.cs:

private List<Image> imgsCarrousel = new List<Image>();

private void btnDeleteImg(object sender, EventArgs e)
{
    imgsCarrousel.Remove(imgSelected); // I want to achive something like this
}
b5lpy0ml

b5lpy0ml1#

如果您阅读CarouselView的文档,它同时具有Position(当前索引)和CurrentItem(实际对象)的属性

kuhbmx9i

kuhbmx9i2#

解决方案

我用这个来解决我的问题。我把我的代码留在这里了,也许它能帮到别人。
代码页.xaml:

<StackLayout Padding="5">
    <CarouselView x:Name="carousel" HeightRequest="600" IndicatorView="{x:Reference contImgs}" PositionChanged="OnPositionChanged">
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <Image x:Name="activeImg" Source="{Binding Source}" Aspect="AspectFill"></Image>
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>
    <IndicatorView x:Name="contImgs" IndicatorColor="#bbb" SelectedIndicatorColor="#000" IndicatorSize="12" IndicatorsShape="Circle"></IndicatorView>
    <ImageButton Source="drawable/icon_trash.png" WidthRequest="100" Clicked="btnDeleteImg" HeightRequest="100" AbsoluteLayout.LayoutBounds="0.98,0.07,50,50" AbsoluteLayout.LayoutFlags="PositionProportional"></ImageButton>
</StackLayout>

代码页. xaml.cs:

private List<Image> imgsCarrousel = new List<Image>();
private int currentPositionImg;
    private void btnDeleteImg(object sender, EventArgs e)
    {
        imgsCarrousel.Remove(currentPositionImg);
        carousel.ItemsSource = imgsCarrousel.ToArray();
    }
    void OnPositionChanged(object sender, PositionChangedEventArgs e)
    {
        int previousItemPosition = e.PreviousPosition;
        currentPositionImg = e.CurrentPosition;
    }

相关问题