[FIXED] How to get the source of the Active image in C# xamarin forms android?

Issue

I’m trying to remove an image from my CarouselView when the user touches the ImageButton. But I don’t know how can I get the active image in my CarouselView.

Does anyone know how could I do that?

Thank you very much!

code Page.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>

code Page.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
}

Solution

SOLUTION

I used this to resolve my problem. I left my code here, maybe it helps someone.

code Page.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>

code Page.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;
    }

Answered By – F205

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published