簡體   English   中英

如何獲取 C# xamarin forms android 中的活動圖像的來源?

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

當用戶觸摸 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
}

如果您閱讀CarouselView文檔,它具有Position (當前索引)和CurrentItem (實際對象)的屬性

解決方案

我用它來解決我的問題。 我把我的代碼留在這里,也許它可以幫助某人。

代碼頁.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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM