Xamarin表单ListView缓存策略
我最近在Xamarin Forms应用程序中测试了CachingStrategy
中ListView
的列表中有1000个项目.列表项是从ViewCell
数据模板创建的.我尝试对CachingStrategy
使用RecycleElement
选项.
I have recently tested the CachingStrategy
for a ListView
in a Xamarin Forms App with 1000 items in the list. List items are being created from a data template of ViewCell
. I Tried using RecycleElement
option for CachingStrategy
.
当我使用Xamarin Profiler对在Xamarin Anroid Player(模拟器)上部署的Android App进行性能分析时,我注意到当滚动浏览列表时,内存分配不会增加(在分配摘要"选项卡上).但是,当我在模拟器上对iPhone App进行性能分析时,我注意到分配摘要"选项卡上没有显示任何数据.因此,我在滚动列表时捕获了一些快照,每当滚动列表(上下)时都会注意到,内存分配不断增加.
When I did profiling, using Xamarin Profiler, for Android App deployed on Xamarin Anroid Player (emulator) I noticed that when I scroll through the list the memory allocation doesn't increase (on allocations summary tab). But, when I did profiling for iPhone App on emulator, I noticed that no data is being displayed on Allocations Summary tab. So I captured some snapshots while scrolling through the list and noticed whenever I scroll through the list (up or down), the memory allocation keeps increasing.
为什么RecycleElement
不适用于iOS(iPhone)?
Why RecycleElement
is not working for iOS (iPhone)?
我正在使用Mac进行开发. 这是我的工具:
I am using Mac for development. Here are my tools:
=== Xamarin Studio ===
=== Xamarin Studio ===
版本5.10.1(内部版本3) 安装UUID:7ae992a3-b710-4297-ba1d-0c519fbb2ea8 运行: 单声道4.2.1(explicit/6dd2d0d) GTK + 2.24.23(罗利主题)
Version 5.10.1 (build 3) Installation UUID: 7ae992a3-b710-4297-ba1d-0c519fbb2ea8 Runtime: Mono 4.2.1 (explicit/6dd2d0d) GTK+ 2.24.23 (Raleigh theme)
Package version: 402010102
=== Xamarin.Profiler ===
=== Xamarin.Profiler ===
版本:0.24.0.0 位置:/Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler
Version: 0.24.0.0 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler
=== Apple开发人员工具===
=== Apple Developer Tools ===
Xcode 7.1.1(9081) 版本7B1005
Xcode 7.1.1 (9081) Build 7B1005
=== Xamarin.iOS ===
=== Xamarin.iOS ===
版本:9.2.1.54(企业版) 哈希:eb4c1ef 分公司:硕士 建立日期:2015-12-01 02:12:30-0500
Version: 9.2.1.54 (Enterprise Edition) Hash: eb4c1ef Branch: master Build date: 2015-12-01 02:12:30-0500
=== Xamarin.Android ===
=== Xamarin.Android ===
版本:6.0.0.34(企业版) Android SDK:/Users/haider/Library/Developer/Xamarin/android-sdk-macosx 支持的Android版本: 4.0.3(API级别15) 4.4(API级别19) 5.0(API级别21) 5.1(API级别22) 6.0(API级别23)
Version: 6.0.0.34 (Enterprise Edition) Android SDK: /Users/haider/Library/Developer/Xamarin/android-sdk-macosx Supported Android versions: 4.0.3 (API level 15) 4.4 (API level 19) 5.0 (API level 21) 5.1 (API level 22) 6.0 (API level 23)
SDK工具版本:24.4.1 SDK平台工具版本:23.1 rc1 SDK Build Tools版本:23.0.2
SDK Tools Version: 24.4.1 SDK Platform Tools Version: 23.1 rc1 SDK Build Tools Version: 23.0.2
Java SDK:/usr Java版本"1.7.0_71" Java(TM)SE运行时环境(内部版本1.7.0_71-b14) Java HotSpot(TM)64位服务器VM(内部版本24.71-b01,混合模式)
Java SDK: /usr java version "1.7.0_71" Java(TM) SE Runtime Environment (build 1.7.0_71-b14) Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
=== Xamarin Android Player ===
=== Xamarin Android Player ===
版本:0.6.5 位置:/Applications/Xamarin Android Player.app
Version: 0.6.5 Location: /Applications/Xamarin Android Player.app
=== Xamarin.Mac ===
=== Xamarin.Mac ===
版本:2.4.0.109(简化版)
Version: 2.4.0.109 (Starter Edition)
===构建信息===
=== Build Information ===
版本ID:510010003 Git修订版:f2021a209d66d49cbc0649a6d968b29040e57807 建立日期:2015-12-01 10:43:40-05 Xamarin插件:dfd4f5103e8951edbc8ac24480b53b53c55e04ff 构建通道:monodevelop-lion-cycle6-baseline
Release ID: 510010003 Git revision: f2021a209d66d49cbc0649a6d968b29040e57807 Build date: 2015-12-01 10:43:40-05 Xamarin addins: dfd4f5103e8951edbc8ac24480b53b53c55e04ff Build lane: monodevelop-lion-cycle6-baseline
===操作系统===
=== Operating System ===
Mac OS X 10.11.1 Darwin Haiders-MacBook-Pro.local 15.0.0 Darwin内核版本15.0.0 2015年9月19日星期六15:53:46 根目录:xnu-3247.10.11〜1/RELEASE_X86_64 x86_64
Mac OS X 10.11.1 Darwin Haiders-MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0 Sat Sep 19 15:53:46 PDT 2015 root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
这里有几件事要检查
-
在Xamarin Profiler中,确保仅在查找自定义ViewCell类,并拍摄多个快照以触发垃圾收集器.如果ViewCells的数量没有增加,则可能是导致内存泄漏的其他原因.如果ViewCells的数量在增加,请转到建议2和. 3,下面. Xamarin Profiler ViewCell示例
在ViewCell代码中,确保覆盖OnBindingContextChanged()
并在OnBindingContextChanged()
中而不是在ViewCell的构造函数中设置控件的属性.我在下面添加了一些示例代码,这些代码演示了如何使用自定义ViewCell来实现ListViewCachingStrategy.RecycleElement
策略.
In the ViewCell code, make sure to override OnBindingContextChanged()
and set the controls' properties in OnBindingContextChanged()
, rather than in the ViewCell's constructor. I added some sample code below that shows how to implement the ListViewCachingStrategy.RecycleElement
strategy using a custom ViewCell.
使用RecycleElement的ListView
ListView = new ListView(ListViewCachingStrategy.RecycleElement)
{
DataTemplate(typeof(CustomViewCell))
};
ViewCell
public class CustomViewCell : ViewCell
{
Label _myLabel;
MenuItem _deleteAction;
public CustomViewCell()
{
_myLabel = new Label();
View = _myLabel;
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
_myLabel.Text = "";
var item = BindingContext as MyModel;
if (item != null)
{
_myLabel.Text = item.Text;
}
}
protected override void OnAppearing()
{
base.OnAppearing();
//Subscribe ViewCell Event Handlers
_deleteAction.Clicked += HandleDeleteClicked;
ContextActions.Add(_deleteAction);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
//Unsubscribe ViewCell Event Handlers
_deleteAction.Clicked -= HandleDeleteClicked;
ContextActions.Remove(_deleteAction);
}
void HandleDeleteClicked(object sender, EventArgs e)
{
//Code to handle when the delete action is tapped
}
}
ViewCell模型
public class MyModel
{
[PrimaryKey]
public int ID { get; set; }
public string Text { get; set; }
}