CarouselView内存不足异常 [英] CarouselView Out Of Memory Exception
问题描述
我目前正在将Xamarin形式的Carousel View与ItemTemplate一起使用,其中包含模板列表,例如
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];
}
这些视图中将包含Xamarin Image控件.图像文件大小大约为10 KB.
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.
推荐答案
不要将所有图像存储在内存中.您必须为所有图像控件设置图像源.它将显示内存不足异常.
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.
例如:
您可以将图像存储在您设置的文件和内存缓存中,
you can store your image in the file and memory cache u set,
我们可以将内存缓存定义为
We can defined the memory cache as
List<Bitmap> bitmapList;
设置bitmapList可以存储4张图像
set the bitmapList can store 4 images
当您移至单个CarouselPage时,只需从"bitmapList"中将图像源设置为图像控件即可.
when you move to the single CarouselPage just set the image source to your image control from the "bitmapList".
如果找不到图像,请从文件存储缓存中获取图像文件,并从"bitmapList"中删除无用的图像,并保持大小为4
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
如果您对CarouselPage使用相同的布局,则无需创建太多视图(view1,view2,view3 .....),请重用这些视图.
If you use the same layout for the CarouselPage, don't need to create so many views(view1, view2,view3.....) please reuse these view.
这篇关于CarouselView内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!