简体   繁体   English

Xamarin forms CarouselView:子元素并不总是实例化

[英]Xamarin forms CarouselView: child elements are not always instantiated

I am facing a problem, I'm working on a quiz app.我面临一个问题,我正在开发一个测验应用程序。 Long story short, in the previous release, I used the Tabbedpage but it did not fulfill all my expectations.长话短说,在之前的版本中,我使用了 Tabbedpage,但它并没有满足我的所有期望。

So I decided to rewrite my UI with the CarouselView.所以我决定用 CarouselView 重写我的 UI。

At the first question, when the user selects his answer his answer 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 highlighted at the same position但这是我的问题:当答案在问题 1 处突出显示时,由于未知原因,问题 6 也在同一 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 reuses the item in order to save memory.我的想法是 carouselview 重用该项目以保存 memory。 It has 5 items and then reuses them?它有 5 个项目,然后重复使用它们? Does anyone know how to solve it, or how to increase the number of unique elements.有谁知道如何解决它,或者如何增加独特元素的数量。

CarouselView is based on CollectionView , so this behavior is expected. CarouselView是基于CollectionView的,所以这种行为是意料之中的。

Please see this comment: XF CarouselView #9200 issue comment请查看此评论: XF CarouselView #9200 问题评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM