C# 如何从代码隐藏设置旋转视图的项目?
我有一个旋转视图,它绑定到一个项目图像源。 但是我想通过更改旋转视图的索引来更改当前显示的图像 我已经尝试使用CarouselView.Position来创建必须选择的元素的索引。但不幸的是,这不起作用 我怎样才能做到这一点? 谢谢 我已经尝试使用CarouselView.Position来创建必须选择的元素的索引。但不幸的是,这不起作用 由于您正在对C# 如何从代码隐藏设置旋转视图的项目?,c#,android,ios,xamarin,xamarin.forms,C#,Android,Ios,Xamarin,Xamarin.forms,我有一个旋转视图,它绑定到一个项目图像源。 但是我想通过更改旋转视图的索引来更改当前显示的图像 我已经尝试使用CarouselView.Position来创建必须选择的元素的索引。但不幸的是,这不起作用 我怎样才能做到这一点? 谢谢 我已经尝试使用CarouselView.Position来创建必须选择的元素的索引。但不幸的是,这不起作用 由于您正在对CarouselView的ItemsSource使用数据绑定,因此可以为图像模型实现INotifyPropertyChanged接口 例如: &l
CarouselView
的ItemsSource
使用数据绑定,因此可以为图像模型实现INotifyPropertyChanged
接口
例如:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
x:Class="FormsIssue2.Page1">
<Grid>
<cv:CarouselView ItemsSource="{Binding Zoos, Mode=OneWay}" x:Name="CarouselZoos">
<cv:CarouselView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Grid.RowSpan="2" Aspect="AspectFill" Source="{Binding ImageUrl, Mode=OneWay}" />
<StackLayout Grid.Row="1" BackgroundColor="#80000000" Padding="12">
<Label TextColor="White" Text="{Binding Name, Mode=OneWay}" FontSize="16" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
</StackLayout>
</Grid>
</DataTemplate>
</cv:CarouselView.ItemTemplate>
</cv:CarouselView>
</Grid>
</ContentPage>
谢谢你的帮助。但我面临的问题是,我有几个缩略图,所有这些缩略图也都绑定到flipview。当用户点击缩略图时,我会使FlipView可见,其中图像以较大的格式显示。但我无法在FlipView中为点击的项目设置项目。通常情况下,我可以使FlipView可见并滑动以四处移动并查看其他图像。只是我无法打开中间的项目。如果这有意义的话。@AbsoluteSith,听起来您想在选择缩略图时在特定位置插入一个项目?您有两个
CarouselView
,一个用于缩略图,另一个用于更大格式的图像?我的演示演示了如何在选中CarouselView
项时更改图像。但由于我使用的是ItemSource绑定,因此在选中CarouselView时不需要设置图像。但这些旋转视图只是按顺序显示图像。这只是我想在那个序列中选择一个特定的位置。@AbsoluteSith,那么,你的意思是当你在缩略图中选择一个项目时,你想让CarouselView
自动滚动到特定的项目?正确。就像任何照片应用程序一样。
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
Zoos = new ObservableCollection<Zoo>
{
new Zoo
{
ImageUrl = "http://wallpaper-gallery.net/images/image/image-13.jpg",
Name = "Woodland Park Zoo"
},
new Zoo
{
ImageUrl = "https://s3-us-west-1.amazonaws.com/powr/defaults/image-slider2.jpg",
Name = "Cleveland Zoo"
},
new Zoo
{
ImageUrl = "http://i.stack.imgur.com/WCveg.jpg",
Name = "Phoenix Zoo"
}
};
//CarouselZoos.ItemsSource = Zoos;
this.BindingContext = this;
CarouselZoos.ItemSelected += CarouselZoos_ItemSelected;
}
private void CarouselZoos_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as Zoo;
if (item == null)
return;
item.ImageUrl = "https://3.bp.blogspot.com/-W__wiaHUjwI/Vt3Grd8df0I/AAAAAAAAA78/7xqUNj8ujtY/s1600/image02.png";
}
public ObservableCollection<Zoo> Zoos { get; set; }
}
public class Zoo : INotifyPropertyChanged
{
private string _ImageUrl;
public string ImageUrl
{
get { return _ImageUrl; }
set
{
if (value != _ImageUrl)
{
_ImageUrl = value;
OnPropertyChanged("ImageUrl");
}
}
}
private string _Name;
public string Name
{
get { return _Name; }
set
{
if (value != _Name)
{
_Name = value;
OnPropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
var postion = CarouselThunbnails.Position;
CarouselImages.Position = postion;