【问题标题】:Xamarin Forms listview out of memory errorXamarin Forms listview 内存不足错误
【发布时间】:2017-03-29 22:24:40
【问题描述】:

在 Xamarin 表单中,我有以下列表视图:

<ListView x:Name="StudentView"  RowHeight="55"  SeparatorVisibility="None" CachingStrategy="RecycleElement">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="55"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <Image Source="{Binding Image}" Grid.Row="0" Grid.Column="0" Aspect="AspectFill"></Image>
                    <Image Source="{Binding Image}" Grid.Row="0" Grid.Column="1" Aspect="AspectFill"></Image>
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

在后面的代码中,我声明了一个包含 1000 个项目的数组。

问题是,如果我向上和向下滚动列表视图,每次我在 xamarin 配置文件中看到内存使用量增加并且似乎 CachingStrategy="RecycleElement" 无法正常工作(在真实的 android 设备上测试)。

滚动几次后,程序因内存不足而崩溃。

有什么问题?我该如何解决?

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    不确定您是如何设置列表视图的 ItemSource 或它是什么。为此还显示一些代码隐藏文件会很有用。

    作为盲目猜测,如果您的来源不可索引,则回收可能会出现性能问题。 Xamarin 的建议是对 itemsource 使用 IList,使用类似 IEnumerable 之类的东西会导致大型数组出现性能问题,因为它本质上必须不断循环数据才能找到要显示的正确项目。

    【讨论】:

      【解决方案2】:

      1000 图片项目很多!我敢打赌这是 Android,我建议使用不同的解决方案,例如:

      1.- 分页策略(加载 10,加载另一个 10 等等)。 2.- 减小图像尺寸。 3.- 使用可缓存的图像,例如: https://github.com/luberda-molinet/FFImageLoading 4.- 使用 Recycler 视图实现 View 自定义渲染器。

      然后关于如何提高 ListView 性能的话题越来越大,这里有一些有用的链接:

      -https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/performance/

      -https://blog.xamarin.com/creating-highly-performant-smooth-scrolling-android-listviews/

      -https://blog.xamarin.com/tips-for-creating-a-smooth-and-fluid-android-ui/

      -http://kent-boogaart.com/blog/jason-smith's-xamarin-forms-performance-tips

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,大量图像或高分辨率图像导致了这个问题,我通过使用FFImageLoading 找到了here 解决了这个问题,也可以在nuget 上找到,也尝试绑定缩略图而不是如果可能,实际图像。

        <ContentPage xmlns:ci="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms">
        <ci:CachedImage Source="{Binding ActualImageThumbnail}" Aspect="Fill"/> 
        

        【讨论】:

        • 如果图像不同也可以工作?例如,我有 1000 个不同的图像,我认为 FFImageLoading 库缓存了所有图像。我遇到的问题是在上下滚动期间,反之亦然,内存增加并且不保持不变(在列表视图中使用回收策略)
        • 是的,它会起作用,我有一个包含大量唯一图像(但少于 200 个)的列表视图,当向上和向下滚动时,它会导致 outOfMemory 异常。此外,我不建议将 1000 项加载到列表视图中,尤其是在会降低应用程序性能的移动设备中,我将采用一些分页策略并使用缩略图来减少内存消耗
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-24
        • 2016-03-18
        • 2021-10-10
        • 2019-04-23
        • 2017-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多