2017-01-30 41 views
0

我使用Xamarin Forms v2.3.1.114,我想在我的應用程序中創建一個調查頁面。Xamarin Forms Carousel查看丟失的選定值(Survey Sample)

我的基本類是這樣的:

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> 

survey app

我用卡羅uselView控件從Nuget中,但是當我從一個列表視圖中選擇一個值並滑動輪播時,所選值已丟失。

我該如何解決這個問題?

謝謝!

+0

你做你的'的ObservableCollection Questions'全局或局部剛? –

回答

1

由於旋轉木馬項目被重用,你必須要更新選定的項目或綁定到你的[輪播項目的新的屬性,你需要添加像這樣:

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">