當用戶觸摸 ImageButton 時,我試圖從CarouselView中洗掉影像。但我不知道如何在 CarouselView 中獲取活動影像。
有誰知道我該怎么做?
非常感謝!
代碼 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>
代碼 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
}
uj5u.com熱心網友回復:
如果您閱讀 的檔案,CarouselView
它具有Position
(當前索引)和CurrentItem
(實際物件)的屬性
uj5u.com熱心網友回復:
解決方案
我用它來解決我的問題。我把我的代碼留在這里,也許它可以幫助某人。
代碼 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>
代碼 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507669.html
標籤:C# xamarin xamarin.forms xamarin.android
上一篇:在Xamarin表單中將x:DataType設定為ViewModel時,CollectionView專案不會更新
下一篇:SQL-如何計算行中的某個字符