2016-11-17 95 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問題 –