【问题标题】:Windows Phone 7 equivalent to NSNotificationCenter?Windows Phone 7 相当于 NSNotificationCenter?
【发布时间】:2010-12-09 13:22:50
【问题描述】:

我是 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");

【问题讨论】:

    标签: iphone windows-phone-7 mvvm-light messages nsnotificationcenter


    【解决方案1】:

    以下是如何使用 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);
    

    【讨论】:

    • 啊,谢谢。所以我必须使用我的 Notification-Id 作为 Token 在 MVVM 中注册/发送,对吗?
    • 没错。令牌可以是任何类型的 Object
    • Thx,所以 MVVM 有点绕,我喜欢 ... Messenger.Default.Send(context-object, NotificationId) ... 这就是为什么我有点困惑.再次感谢。
    【解决方案2】:

    在这里http://www.silverlightshow.net/items/Implementing-Push-Notifications-in-Windows-Phone-7.aspx你会找到一个关于如何在windows phone 7上使用推送通知的很好的例子。

    【讨论】:

    • 据我所知推送通知是基于在线的,对吗?或者是否有离线推送通知,如适用于 WP7 的 iOS 上的本地通知?因为我只需要应用内的通知,不需要在线连接。
    • 据我所知,这仅适用于在线通知。我不知道是否有任何离线,但我认为没有。这意味着多任务处理,您的应用程序将在后台运行。
    【解决方案3】:

    我很确定您通过创建一个单例来归档与 NSNotificationCenter 相同的结果,该单例包含一个可观察对象列表,该列表根据您的业务需求实现特定接口,或者为每条消息调用一个lamba,或触发一个事件由这个单例发送,你将交互观察列表并检查消息 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!");
        }
     }
    

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多