Silverlight WP7 消息传递,我应该使用 WeakReference 吗?如果是的话,如何使用?
我正在为 Windows Phone silverlight 应用程序创建一个简单的消息系统。
这个想法是各种 xaml 页面和其他对象将订阅消息传递对象,传递它们想要接收/处理的消息类型和动作<>。委托作为处理程序。
当操作发生时,一条消息(带有有效负载)将被发送给正确的订阅者。
这是我想要的消息类的快速草稿。
public class MessageBus
{
private List<Subscriber> subscribers;
public MessageBus()
{
subscribers = new List<Subscriber>();
}
public void Subscribe(string messageType, Action<object>subscriber){
subscribers.Add(new Subscriber(messageType, subscriber));
}
public void SendMessage(object message, string messageType)
{
foreach (Subscriber subscriber in subscribers)
{
if (subscriber.MessageType == messageType && subscriber.Reciever != null)
{
subscriber.Reciever(message);
}
}
}
}
public class Subscriber
{
public string MessageType { get; set; }
public Action<object> Reciever { get; set; }
public Subscriber(string messageType, Action<Object> reciever)
{
MessageType = messageType;
Reciever = reciever;
}
}
因此,不同的订阅者会为自己添加一个类型:Action。据我了解,这将阻止原始页面/对象被垃圾收集(我认为否则会是这样?),因为对它的引用将始终存在。
我无法真正取消订阅,或者无论如何也不能总是取消订阅,并且消息队列将在应用程序的生命周期内保留。
我应该实施 WeakReferences 吗?如果是的话如何实施?
弱引用会增加更多开销吗?
我是否疯狂地考虑这个,因为使用的内存很小?
I'm creating a simple messaging system for a windows phone silverlight app.
The idea is various xaml pages & other objects will subscribe to a messaging object, passing in the type of message they want to recieve/handle and an Action<> Delegate as the handler.
When an action happens a message (with payload) will be sent to the correct subscribers.
Here's a quick draft of what I want as the message class.
public class MessageBus
{
private List<Subscriber> subscribers;
public MessageBus()
{
subscribers = new List<Subscriber>();
}
public void Subscribe(string messageType, Action<object>subscriber){
subscribers.Add(new Subscriber(messageType, subscriber));
}
public void SendMessage(object message, string messageType)
{
foreach (Subscriber subscriber in subscribers)
{
if (subscriber.MessageType == messageType && subscriber.Reciever != null)
{
subscriber.Reciever(message);
}
}
}
}
public class Subscriber
{
public string MessageType { get; set; }
public Action<object> Reciever { get; set; }
public Subscriber(string messageType, Action<Object> reciever)
{
MessageType = messageType;
Reciever = reciever;
}
}
So varius subscribers will add themselves with a type, Action. As I understand this will stop the original pages/objects from being garbage collected (I assume it would be otherwise?) because a reference to it will always exist.
I can't really unsubscribe, or not always anyway and the messaging queue will stay around for the lifetime of the application.
Should I implement WeakReferences and if so how?
Would WeakReferences add more overhead?
Am I crazy to even consider this because the memory in use will be tiny?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MVVM Light 工具包具有出色的松散耦合消息总线,可用于 WP7
http://www.galasoft.ch/mvvm/getstarted/
您可能需要这个:
http://blog.galasoft.ch/archive/2010/07/22/mvvm-light-hotfix-for-windows-phone-7-developer-tools-beta.aspx
The MVVM Light toolkit has a fantastic loosely coupled messaging bus and it's available for WP7
http://www.galasoft.ch/mvvm/getstarted/
You may need this:
http://blog.galasoft.ch/archive/2010/07/22/mvvm-light-hotfix-for-windows-phone-7-developer-tools-beta.aspx