简体   繁体   English

如何优化 .NET MAUI 中的列表视图数据绑定?

[英]How can I optimize listview data binding in .NET MAUI?

I am working on a MAUI app, which has a page with two ListViews.我正在开发一个 MAUI 应用程序,它有一个包含两个 ListView 的页面。 Only one should be visible at a time, which i do by setting their IsVisible property to true or false.一次只有一个应该可见,我通过将它们的 IsVisible 属性设置为 true 或 false 来做到这一点。 Upon switching from one to the other, the app freezes or becomes extremely slow.从一个切换到另一个后,应用程序会冻结或变得非常慢。 I suppose this is because the listview's itemssource is rather large.我想这是因为列表视图的项目源相当大。 I bound both listviews to an observablecollection, which should be the fastest option.我将两个列表视图绑定到一个 observablecollection,这应该是最快的选择。 Both ObservableCollections have data that updates rather often, and both should get quite big (1000+ items).两个 ObservableCollections 都有更新相当频繁的数据,并且都应该变得相当大(1000 多个项目)。 How can I optimize the listviews to load quickly?如何优化列表视图以快速加载?

I have tried to limit the itemssource to a certain amount of objects, but this only makes it slower.我试图将 itemssource 限制为一定数量的对象,但这只会让它变慢。 I guess this is because removing items from the itemssource in a first-in-first-out way basically updates every single object within the obervablecollection.我想这是因为以先进先出的方式从 itemssource 中删除项目基本上会更新 obervablecollection 中的每个 object。

My current code: `我当前的代码:`

<Grid RowDefinitions="*,5,40" ColumnDefinitions="75,75,75,105,*">
                    <ScrollView x:Name="SerialLogScrollViewASCII"
                            Grid.ColumnSpan="6"
                            VerticalScrollBarVisibility="Always"
                            Grid.Row="0"
                            IsVisible="{Binding IsASCIILogVisible}"
                            BackgroundColor="Transparent">

                        <ListView x:Name="LogStackASCII"
                            ItemsSource="{Binding SerialLogASCII}"
                            BackgroundColor="Transparent"
                                  
                            ItemTapped="LogMessageTapped"
                            SelectionMode="Single">
                            <ListView.ItemTemplate>
                                <DataTemplate >
                                    <ViewCell>
                                        <Grid PropertyChanged="SerialLogChanged" ColumnDefinitions="160,*" ColumnSpacing="5" BackgroundColor="Transparent">
                                            <Label Text="{Binding Time}" Margin="0,0,10,0" Grid.Column="0" FontSize="12" BackgroundColor="Transparent"/>
                                            <Label Text="{Binding Message}" LineBreakMode="CharacterWrap" Grid.Column="1" FontSize="12" BackgroundColor="Transparent"/>
                                        </Grid>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </ScrollView>
                   
                    <ScrollView x:Name="SerialLogScrollViewHEX"
                            Grid.ColumnSpan="6"
                            VerticalScrollBarVisibility="Always"
                            Grid.Row="0"
                            IsVisible="{Binding IsHEXLogVisible}"
                            BackgroundColor="Transparent">
                        <ListView x:Name="LogStackHEX"
                            ItemsSource="{Binding SerialLogHEX}"
                            BackgroundColor="Transparent"                                  
                            ItemTapped="LogMessageTapped"
                            SelectionMode="Single">
                            <ListView.ItemTemplate>
                                <DataTemplate >
                                    <ViewCell>
                                        <Grid PropertyChanged="SerialLogChanged" Padding="1" ColumnDefinitions="160,*" ColumnSpacing="5" BackgroundColor="Transparent">
                                            <Label Text="{Binding Time}" Margin="0,0,10,0" Grid.Column="0" FontSize="12" BackgroundColor="Transparent"/>
                                            <Label Text="{Binding Message}" LineBreakMode="CharacterWrap" Grid.Column="1" FontSize="12" BackgroundColor="Transparent"/>
                                        </Grid>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </ScrollView>
                </Grid>

` `

You could try Caching strategy in ListView.您可以在 ListView 中尝试缓存策略 Try the following code试试下面的代码

<ListView x:Name="LogStackASCII" CachingStrategy="RecycleElement"
...

RecycleElement should be used in the following circumstances: RecycleElement应在以下情况下使用:

  1. Each cell has a small to moderate number of bindings.每个单元格都有少量到中等数量的绑定。
  2. Each cell's BindingContext defines all of the cell data.每个单元格的 BindingContext 定义所有单元格数据。
  3. Each cell is largely similar, with the cell template unchanging.每个单元格在很大程度上是相似的,单元格模板不变。

I found the Xamarin official docs ListView performance for you.给你找了Xamarin官方文档ListView performance It also works for Maui .它也适用于毛伊岛 Besides the Caching strategy, it also gives some other ListView performance suggestions to enhance performance of Listview.除了Caching策略,它还给出了其他一些ListView性能建议,以提升Listview的性能。

By the way, the two ListView almost have the same template, so why not just use one ListView?顺便说一下,两个ListView的模板几乎是一样的,为什么不只用一个ListView呢? And when switching, just change the itemsSource it binds to.而在切换时,只需更改它绑定的 itemsSource 即可。

Hope it works for you.希望对你有效。

  1. When you put Vertical ListView, inside Vertical ScrollView, the performance is pretty much non-existent.当您将 Vertical ListView 放在 Vertical ScrollView 中时,性能几乎不存在。 The more and more items you add, the worse it will get.您添加的项目越多,情况就越糟。

You should not be doing this.你不应该这样做。 ListView has its own scroll. ListView 有自己的滚动条。

  1. Consider CollectionView or DataGrid.考虑 CollectionView 或 DataGrid。

First remove the scolls.首先删除scolls。 Then if you are not happy, try CollectionView or DataGrid.然后,如果您不满意,请尝试 CollectionView 或 DataGrid。 And then if you have questions, please ask.然后,如果您有任何问题,请提问。

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

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