Home > Back-end >  How to get the source of the Active image in C# xamarin forms android?
How to get the source of the Active image in C# xamarin forms android?

Time:08-30

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
}

CodePudding user response:

if you read the docs for CarouselView, it has properties for both Position (the current index) and CurrentItem (the actual object)

CodePudding user response:

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;
    }
  • Related