question

JohnnyValery-7294 avatar image
0 Votes"
JohnnyValery-7294 asked ColeXia-MSFT commented

xamarin forms CarouselView: chield element are reused

I face a problem, I'm working on a quiz app. To make a long story short, I'n the previous release, I used the Tabbedpage but it did not fill all my expectation So I decide to rewrite my UI with the CarouselView.

At the first question, when my user select his answer his anwser is highlighted (green if it's correct, red if it's wrong).

But here is my problem: When the answer is highlighted at the question 1, for an unknown reason the question 6 is also higlighted at the same position

 <CarouselView x:Name="Carrousel" 
                           Loop="False" 
                           ItemsSource="{Binding FullQuestion}" 
                           IndicatorView="indicatorView"
                           >
                 <CarouselView.ItemTemplate>
                     <DataTemplate>
                         <Grid x:Name="FirstGrid">
                             <Grid.RowDefinitions>
                                 <RowDefinition Height="auto"/>
                                 <RowDefinition Height="0.3*"/>
                                 <RowDefinition Height="auto"/>
                                 <RowDefinition Height="0.3*"/>
                                 <RowDefinition Height="*"/>
                         </Grid.RowDefinitions>
                             <Label x:Name="Question" Text="{Binding Name}" />
                             <views:MusicButtonView x:Name="MusicButtonView" Grid.Row="1" Margin="10" SongUrl="{Binding ID}" />
                             <Label x:Name="QuestionType" Grid.Row="2" Text="{Binding NameOfQuestion}" />
                         <Grid x:Name="ButtonGrid" Grid.Row="4">
                             <Grid.RowDefinitions>
                                 <RowDefinition Height="*"/>
                                 <RowDefinition Height="*"/>
                                 <RowDefinition Height="*"/>
                                 <RowDefinition Height="*"/>
                             </Grid.RowDefinitions>
                             <Button Grid.Row="0" x:Name="btn1" Text="{Binding Lines[0].SongName}" Clicked="OnBtnClick" BackgroundColor="#998A8B8D" BorderColor="White" CornerRadius="1" BorderWidth="2"/>
                             <Button Grid.Row="1" x:Name="btn2" Text="{Binding Lines[1].SongName}" Clicked="OnBtnClick" BackgroundColor="#998A8B8D" BorderColor="White" CornerRadius="1" BorderWidth="2"/>
                             <Button Grid.Row="2" x:Name="btn3" Text="{Binding Lines[2].SongName}" Clicked="OnBtnClick" BackgroundColor="#998A8B8D" BorderColor="White" CornerRadius="1" BorderWidth="2"/>
                             <Button Grid.Row="3" x:Name="btn4" Text="{Binding Lines[3].SongName}" Clicked="OnBtnClick" BackgroundColor="#998A8B8D" BorderColor="White" CornerRadius="1" BorderWidth="2"/>
                         </Grid>
                     </Grid>
                     </DataTemplate>
                 </CarouselView.ItemTemplate>
             </CarouselView>

 private void Process(QuestionFull data, int index,ref Button btn)
         {
             var id = data.QuestionId;
                
             if(!data.IsAnswered)
             {
                 btn.BackgroundColor = Color.FromHex("#998A8B8D");
             }
    
             if (data.ID.Equals(data.Lines[index].SongId))
             {
                 btn.BackgroundColor = Color.FromRgb(0, 213, 33);
                 data.IsCorrectAnswer = true;
             }
             else
             {
                 btn.BackgroundColor = Color.FromRgb(255, 69, 56);
                 data.IsCorrectAnswer = false;
             }
             data.IsAnswered = true;
         }

My idea is that carouselview reuse the item in order to save memory. It has 5 items and the reuse it? Does any one know hot to solve it, or how to increase the number of unique element.

dotnet-csharpdotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Where to call Process ? Could you provide us the complete code to test ?

0 Votes 0 ·

0 Answers