我使用Xamarin Forms v2.3.1.114,我想在我的应用程序中创建一个调查页面。
我的基础课是:
public class Question
{
public string Title { get; set; }
public List<string> Answers { get; set; }
}
我的示例代码是:
ObservableCollection<Question> Questions = new ObservableCollection<Question> ();
Questions.Add (
new Question () {
Title = "Question A",
Answers = new List<string>{
"Answer A",
"Answer B",
"Answer C",
"Answer D"
}
}
);
Questions.Add (
new Question () {
Title = "Question B",
Answers = new List<string>{
"Answer A",
"Answer B",
"Answer C",
"Answer D",
"Answer E",
"Answer F"
}
}
);
我的Xaml如下:
<Grid Grid.Row="1">
<cv:CarouselView x:Name="CarouselQuestions">
<cv:CarouselView.ItemTemplate>
<DataTemplate>
<Grid Padding="12">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" BackgroundColor="#404040">
<ContentView Padding="12">
<Label TextColor="White" HorizontalOptions="Start" Text="{Binding Title}"/>
</ContentView>
</Grid>
<ListView Grid.Row="1" ItemsSource="{Binding Answers}" BackgroundColor="Transparent" HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="12">
<Label TextColor="Black" Text="{Binding}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</cv:CarouselView.ItemTemplate>
</cv:CarouselView>
</Grid>
我使用Nuget的CarouselView控件但是当我从一个列表视图中选择一个值并滑动轮播时,所选的值已经丢失。
我该如何解决这个问题?
谢谢!!
答案 0 :(得分:1)
由于重复使用Carousel项目,您必须更新所选项目或将其绑定到CarouselView项目的新属性中,您需要添加以下内容:
object _selectedItem;
public object SelectedItem{
get{
return _selectedItem;
}
set{
_selectedItem = value;
OnPropertyChanged();
}
}
并将绑定添加到ListView Xaml
<ListView SelectedItem="{Binding SelectedItem}" Grid.Row="1" ItemsSource="{Binding Answers}" BackgroundColor="Transparent" HasUnevenRows="true">