Windows Phone 7相当于NSNotificationCenter?

发布于 2024-10-07 00:11:55 字数 849 浏览 6 评论 0原文

我是 WP7 的新手,之前从事 iPhone 开发工作。在 iPhone 上,我习惯使用 NSNotificationCenter 来通知我的程序一些事情。 NSNotificationCenter 是开箱即用的内置框架。 WP7里有类似的东西吗?我偶然发现了 MVVM-Light Toolkit,但我不确定如何正确使用它。

我想要做的:

  • 注册到Notification-Id并在收到Notification-Id时执行某些操作
  • 发送带有Notification-Id和上下文的通知(传递给观察者的对象)
  • 每个注册到同一Notification-Id的人都会收到通知

所以类似:注册

NotificationCenter.Default.register(receiver, notification-id, delegate);

发送:

NotificationCenter.Default.send(notification-id, context);

注册示例:

NotificationCenter.Default.register(this, NotifyEnum.SayHello, m => Console.WriteLine("hello world with context: " + m.Context));

发送...

NotificationCenter.Default.send(NotifyEnum.SayHello, "stackoverflow context");

I'm new to WP7 and coming from iPhone development. On iPhone I'm used to use NSNotificationCenter to notify my program of something. NSNotificationCenter is build-in the framework out of the box. Is there something similar in WP7? I stumbled uppon MVVM-Light Toolkit but I'm not sure how to use it correctly.

What I want to do:

  • Register to an Notification-Id and do something when Notification-Id is received
  • Send Notification with Notification-Id and a context (object to pass to observers)
  • Everyone who registers to the same Notification-Id will be notified

So something like: Registering

NotificationCenter.Default.register(receiver, notification-id, delegate);

Sending:

NotificationCenter.Default.send(notification-id, context);

Example for Registering:

NotificationCenter.Default.register(this, NotifyEnum.SayHello, m => Console.WriteLine("hello world with context: " + m.Context));

Sending ...

NotificationCenter.Default.send(NotifyEnum.SayHello, "stackoverflow context");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你列表最软的妹 2024-10-14 00:11:55

以下是如何使用 MVVM Light Toolkit:

注册:

Messenger.Default.Register<string>(this, NotificationId, m => Console.WriteLine("hello world with context: " + m.Context));

发送:

Messenger.Default.Send<string>("My message", NotificationId);

Here is how to do with the MVVM Light Toolkit:

Registering:

Messenger.Default.Register<string>(this, NotificationId, m => Console.WriteLine("hello world with context: " + m.Context));

Sending:

Messenger.Default.Send<string>("My message", NotificationId);
溺渁∝ 2024-10-14 00:11:55

这里 http://www.silverlightshow.net/ items/Implementing-Push-Notifications-in-Windows-Phone-7.aspx 您会发现一个关于如何在 Windows Phone 7 上使用推送通知的绝佳示例。

Here http://www.silverlightshow.net/items/Implementing-Push-Notifications-in-Windows-Phone-7.aspx you will find a great example on how to use push notification on windows phone 7.

安静被遗忘 2024-10-14 00:11:55

我非常确定您可以通过创建一个单例来归档与 NSNotificationCenter 相同的结果,该单例包含一个可观察列表,该列表根据您的业务需求实现特定的接口,或者为由此发送的每条消息调用lambda,或者触发一个事件单例中,您将交互可观察列表并检查消息 ID,一旦找到一个或多个,您可以调用接口方法,或执行 lambda 表达式或触发定义的事件来消化消息内容。

像下面这样:

public class NotificationCenter {

    public static NotificationCenter Default = new NotificationCenter();

    private List<KeyValuePair<string, INotifiable>> consumers;

    private NotificationCenter () {

       consumers = new List<INotifiable>();
    }

    public void Register(string id, INotifiable consumer) {

        consumers.Add(new KeyValuePair(id, consumer));
    }

    public void Send(String id, object data) {

        foreach(KeyValuePair consumer : consumers) {

            if(consumer.Key == id)
                consumer.Value.Notify(data);
        } 
    }
 }

 public interface INotifiable {

    void Notify(object data);
 }


 public class ConsumerPage  : PhoneApplicationPage, INotifiable {

    public ConsumerPage() {

       NotificationCenter.Default.Register("event", this);
    }

    private Notify(object data) {

       //do what you want
    }
 }

 public class OtherPage : PhoneApplicationPage {

    public OtherPage() {

        NotificationCenter.Default.Send("event", "Hello!");
    }
 }

I'm pretty sure that you archive the same result as NSNotificationCenter by creating a singleton which holds a list of observables that implements a specific interface based on your bussiness requirements, or call a lamba, or trigger an event, for each message sent by this singleton you will interate the list of observables and checking the message id, once you find one or more, you can call the interface method, or execute the lambda expression or trigger the event defined to digest the message contents.

Something like below:

public class NotificationCenter {

    public static NotificationCenter Default = new NotificationCenter();

    private List<KeyValuePair<string, INotifiable>> consumers;

    private NotificationCenter () {

       consumers = new List<INotifiable>();
    }

    public void Register(string id, INotifiable consumer) {

        consumers.Add(new KeyValuePair(id, consumer));
    }

    public void Send(String id, object data) {

        foreach(KeyValuePair consumer : consumers) {

            if(consumer.Key == id)
                consumer.Value.Notify(data);
        } 
    }
 }

 public interface INotifiable {

    void Notify(object data);
 }


 public class ConsumerPage  : PhoneApplicationPage, INotifiable {

    public ConsumerPage() {

       NotificationCenter.Default.Register("event", this);
    }

    private Notify(object data) {

       //do what you want
    }
 }

 public class OtherPage : PhoneApplicationPage {

    public OtherPage() {

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