2016-11-17 106 views
0

我目前正在使用带有ItemTemplate的Carousel View的Xamarin表单版本,其中有模板列表,例如CarouselView内存不足异常

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图像控件。图像文件大小大致在10k字节左右。

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

回答

3

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

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

例如:

你可以存储在文件和内存缓存ü设置你的形象,

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

List<Bitmap> bitmapList; 

设置bitmapList可存储4张图片

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

如果找不到图像,从你的文件存储缓存中的图像文件,并从“bitmapList”删除无用的图像保持一定的尺寸为4

如果使用相同的布局为CarouselPage,唐不需要创建如此多的视图(view1,view2,view3 .....)请重用这些视图。

+0

尼斯理论..我解决了使用FFImageLoading。我认为它使用了与您相似的想法,谢谢。 – LittleFunny

0

打开位于Android项目的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> 
+0

谢谢,但我最终使用FFImageLoading,它解决了问题 – LittleFunny

+0

FFImageLoading的什么功能解决了内存不足的问题,因为我正在使用它,并仍然在CarouselPage中的图像出现OutOfMemory问题 –