CarouselView 内存不足异常
CarouselView Out Of Memory Exception
我目前正在使用带有 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 控件。图像文件大小大约为 10kbytes。
如何防止内存不足。
不要将所有图像都存储在内存中。您必须为所有图像设置图像源 controls.It 将显示内存不足异常。
为您的图像控件创建图像缓存。
例如:
您可以将图像存储在您设置的文件和内存缓存中,
我们可以定义内存缓存为
List<Bitmap> bitmapList;
设置bitmapList可以存储4张图片
当您移动到单个 CarouselPage 时,只需将图像源设置为来自 "bitmapList" 的图像控件。
如果找不到图像,请从文件存储缓存中获取图像文件,并从 "bitmapList" 中删除无用的图像,保持大小为 4
如果CarouselPage使用相同的布局,不需要创建那么多视图(view1,view2,view3.....)请重复使用这些视图。
打开位于 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>
我目前正在使用带有 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 控件。图像文件大小大约为 10kbytes。
如何防止内存不足。
不要将所有图像都存储在内存中。您必须为所有图像设置图像源 controls.It 将显示内存不足异常。
为您的图像控件创建图像缓存。
例如:
您可以将图像存储在您设置的文件和内存缓存中,
我们可以定义内存缓存为
List<Bitmap> bitmapList;
设置bitmapList可以存储4张图片
当您移动到单个 CarouselPage 时,只需将图像源设置为来自 "bitmapList" 的图像控件。
如果找不到图像,请从文件存储缓存中获取图像文件,并从 "bitmapList" 中删除无用的图像,保持大小为 4
如果CarouselPage使用相同的布局,不需要创建那么多视图(view1,view2,view3.....)请重复使用这些视图。
打开位于 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>