Silverlight WP7 消息传递,我应该使用 Wea​​kReference 吗?如果是的话,如何使用?

发布于 2024-09-11 20:43:12 字数 1370 浏览 4 评论 0原文

我正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2024-09-18 20:43:12

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文