c# - 我是否需要取消订阅 Xamarin.Forms 中的 GestureRecogniser?
问题描述
所以我有以下控制,从Label
public Hyperlink()
{
var tapGesture = new TapGestureRecognizer();
tapGesture.Tapped += TapGesture_Tapped;
this.GestureRecognizers.Add(tapGesture);
}
现在我的问题是,我需要解开挂钩GestureRecogniser
吗?
我对GarbageCollection的理解是,因为Hyperlink
对象负责它,所以tapGesture
它应该能够处理它;但这就是 Xamarin.Forms 的世界。
所以我需要Dispose
代码来避免潜在的内存泄漏并避免让对象保持活动状态吗
public void Dispose()
{
if (this.GestureRecognizers.Count > 0)
{
var tapGesture = this.GestureRecognizers[0] as TapGestureRecognizer;
tapGesture.Tapped -= TapGesture_Tapped;
this.GestureRecognizers.Clear();
}
}
解决方案
根据Cross-Platform Performance,建议您取消订阅 GestureRecogniser。
为防止内存泄漏,应在处理订阅者对象之前取消订阅事件。在取消订阅事件之前,发布对象中的事件委托具有对封装订阅者事件处理程序的委托的引用。只要发布对象持有这个引用,垃圾回收就不会回收订阅者对象的内存。
推荐阅读
- c# - 带有 CollectionAssert.AreEquivalent() 的单元测试列表
- shell - 从文件的同一行中删除重复项
- php - 如何使用 bat 运行 php,使用 cron 作业运行 vbs 脚本
- php - 如何在 PHP 中使用这个 POST CURL
- c# - 如何使用 OpenCL 将位图转换为纯黑白
- list - Haskell 将参数转换为类型
- snowflake-cloud-data-platform - 有没有办法通过Scim Api调用来管理像Accountadmin这样的雪花默认角色
- ios - 如何检查项目是否可见 - SwiftUI ScrollView
- firebase - 如何在云火存储中将我的文档 ID 设置为用户 uid?
- python - 用字典中的冲突键替换字符串中的符号