CarouselView Out Of Memory Exception

[亡魂溺海] 提交于 2019-12-12 03:06:07

问题


I am currently using a xamarin form version of Carousel View with ItemTemplate which have a list of templates e.g.

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];
    }

Inside those views will include Xamarin Image control. The images file size roughly around 10kbytes.

What can I do to prevent from out of memory.


回答1:


Don't store all the images in the memory. You must have set the image source for your all image controls.It will show out of memory exception.

Create image cache for your image controls.

For example:

you can store your image in the file and memory cache u set,

We can defined the memory cache as

List<Bitmap> bitmapList;

set the bitmapList can store 4 images

when you move to the single CarouselPage just set the image source to your image control from the "bitmapList".

If you not find the image, get the image file from your file store cache and remove the useless image from the "bitmapList" keep the size is 4

If you use the same layout for the CarouselPage, don't need to create so many views(view1, view2,view3.....) please reuse these view.




回答2:


Open AndroidManifest.xml file located in Properties folder of Android Project.

Add android:largeHeap="true" in application tag. It will solve OOM issue.

Eg :

<?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>


来源:https://stackoverflow.com/questions/40646450/carouselview-out-of-memory-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!