Windows Phone 7相当于NSNotificationCenter?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是如何使用 MVVM Light Toolkit:
注册:
发送:
Here is how to do with the MVVM Light Toolkit:
Registering:
Sending:
这里 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.
我非常确定您可以通过创建一个单例来归档与 NSNotificationCenter 相同的结果,该单例包含一个可观察列表,该列表根据您的业务需求实现特定的接口,或者为由此发送的每条消息调用lambda,或者触发一个事件单例中,您将交互可观察列表并检查消息 ID,一旦找到一个或多个,您可以调用接口方法,或执行 lambda 表达式或触发定义的事件来消化消息内容。
像下面这样:
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: