MonoTouch中的弱事件模式 [英] Weak Event Pattern in MonoTouch

查看:127
本文介绍了MonoTouch中的弱事件模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于使用Objective-C语言开发iOS应用程序,并依赖 dealloc 方法在我的应用程序中执行一些清理/取消注册任务。现在在MonoTouch(垃圾收集),它不再是一个选项。

I used to develop iOS apps using the Objective-C language, and relied on the dealloc method to perform some cleanup/unregister tasks in my application. Now on the MonoTouch (garbage collected) it is not an option anymore.

假设我有一个 UIViewController 添加作为视图的子视图属性 MyView UIView 子类)。 MyView 反过来注册自己以从另一个管理器/全局对象接收一些事件,以便它知道如何相应地更新自己(例如: onlineProfilesManager.Refreshed + =()=><具有新状态的更新UI< ;;

Suppose I have a UIViewController that adds as a subview of it's View property an instance of MyView (UIView subclass). MyView in turn registers itself to receive some events from another manager/global object so that it knows how to update itself accordingly (e.g.: onlineProfilesManager.Refreshed += () => <update UI with the new state>;).

只要 MyView 在屏幕上,一切都很好。但是,我必须知道什么时候从屏幕上删除,以便我可以从事件处理程序中注销 MyView

As long as MyView is on screen, everything is fine. However I must know when it's removed from the screen so that I can unregister MyView from the event handler.

Obj-C这可以简单地在 dealloc 方法中完成,因为当屏幕更改时,$ code> UIViewController 被释放 - > MyView 从其超级视图中删除,然后调用$ code> MyView dealloc方法。

In Obj-C this could be simply done in the dealloc method because when the screen changes the UIViewController is deallocated --> MyView is removed from it's superview and then MyView dealloc method is called.

在Monotouch,我没有这个'确定性'流。我试图在 UIViewController MyView 析构函数中放置一些打印语句,但是它们永远不会被调用(原因是因为 MyView 仍然注册为事件处理程序,因为我不知道何时/如何取消注册,它将永远不会被释放)。

In Monotouch I don't have this 'deterministic' flow anymore. I tried to put some print statements in the UIViewController and MyView destructors but they are never called (the reason is because the MyView is still registered for the event handler, since I don't know when/how to unregister it, it will never be deallocated).

有没有人知道在MonoTouch中处理这种情况的模式是什么?我想我错过了一个基本的概念,在开发我的应用程序时遇到麻烦。

Does anyone know what is the 'pattern' to handle such situations in MonoTouch? I think I'm missing a fundamental concept and getting into trouble developing my apps.

提前感谢。

编辑
我正在编辑我的问题,因为我的问题的解决方案就是使用弱事件模式,但是我没有找到MonoTouch平台的实现。

EDIT I'm editing my question because looks like the solution for my problem is using the Weak Event Pattern but I didn't find an implementation for the MonoTouch platform.

有谁知道如何使用弱活动模式在MonoTouch?

Does anyone know how can I use the Weak Event Pattern in MonoTouch ?

推荐答案

处理事件的最好方法是在ViewWillDisappear中注销它们,并在ViewWillAppear中注册它们。这意味着您不能使用匿名方法,因为您没有引用该方法来注销它。

The best way to handle events is to unregister them in ViewWillDisappear and register them in ViewWillAppear. This means that you can't use anonymous methods though as you don't have a reference to the method to unregister it.

如果这不符合您需要的,您可以执行类似于此 http://sgmunn.com/blog/2012/05的操作/ non-gcd-event-handlers /

If that doesn't suit what you need, you can do something similar to this http://sgmunn.com/blog/2012/05/non-gcd-event-handlers/

干杯。

这篇关于MonoTouch中的弱事件模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆