CarouselView Out of Memory Exception

时间:2016-11-17 03:55:48

标签: android xamarin xamarin.forms out-of-memory carousel

我目前正在使用带有ItemTemplate的Carousel View的xamarin表单版本,其中包含模板列表,例如。

public class TemplateSelector : DataTemplateSelector
{
    private DataTemplate[] dataTemplates;

    public TemplateSelector()
    {
        dataTemplates = new DataTemplate[] {
            new DataTemplate (typeof (View1)),
            new DataTemplate (typeof (View2)),
            new DataTemplate (typeof (View3)),
            new DataTemplate (typeof (View4)),
            new DataTemplate (typeof (View5)),
            new DataTemplate (typeof (View6)),
            new DataTemplate (typeof (View7)),
            new DataTemplate (typeof (View8)),
            new DataTemplate (typeof (View9)) 
        };
    }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        var page = (WaveOobePage.Page)item;
        return dataTemplates[page.Index];
    }

这些视图中将包含Xamarin Image控件。图像文件大小约为10千字节。

我该怎么做才能防止内存不足。

2 个答案:

答案 0 :(得分:3)

请勿将所有图像存储在内存中。您必须为所有图像控件设置图像源。它将显示内存不足异常。

为图像控件创建图像缓存。

例如:

您可以将图像存储在文件和内存缓存中,

我们可以将内存缓存定义为

List<Bitmap> bitmapList;

设置bitmapList可以存储4个图像

当您移动到单个CarouselPage时,只需将图像源设置为“bitmapList”中的图像控件。

如果找不到图像,请从文件存储缓存中获取图像文件,并从“bitmapList”中删除无用的图像,保持大小为4

如果您对CarouselPage使用相同的布局,则不需要创建这么多视图(view1,view2,view3 .....)请重复使用这些视图。

答案 1 :(得分:0)

打开位于Android Project的Properties文件夹中的AndroidManifest.xml文件。

在应用标记中添加android:largeHeap="true"。它将解决OOM问题。

例如:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Sample.Sample" android:versionCode="1" android:versionName="1.0" android:installLocation="internalOnly">
    <application android:label="Sample" android:largeHeap="true">
    </application>
</manifest>