Question

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");
Was it helpful?

Solution

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);

OTHER TIPS

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.

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!");
    }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top