Pregunta

Soy nuevo en WP7 y procedentes de desarrollo del iPhone. En el iPhone que estoy acostumbrado a utilizar NSNotificationCenter para notificar a mi programa de algo. NSNotificationCenter es construir-en el marco fuera de la caja. ¿Hay algo similar en WP7? Tropecé uppon MVVM Light Toolkit-pero no estoy seguro de cómo utilizar correctamente.

Lo que quiero hacer:

  • Registro a una Notificación-Id y hacer algo cuando se recibe notificación-Id
  • enviar una notificación con la Notificación-Id y un contexto (objeto para pasar a observadores)
  • Todo el que se registra en la misma Notificación-Id se notificará

Así que algo como: Registro

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

Envío:

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

ejemplo de registro:

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

Enviando ...

NotificationCenter.Default.send(NotifyEnum.SayHello, "stackoverflow context");
¿Fue útil?

Solución

Aquí es cómo hacerlo con el MVVM Light Toolkit:

Registro de:

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

Envío:

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

Otros consejos

http: //www.silverlightshow .net / artículos / ejecución push-Notificaciones-en-windows-Phone-7.aspx se encuentra un gran ejemplo de cómo usar la notificación de inserción en windows Phone 7.

estoy bastante seguro de que archive el mismo resultado que NSNotificationCenter mediante la creación de un producto único que mantiene una lista de observables que implementa una interfaz específica en función de sus requisitos del bussiness, o llamar a un lamba, o desencadenar un evento, para cada mensaje enviados por este producto único que se Interate la lista de observables y comprobando el mensaje de identificación, una vez que encuentre uno o más, se puede llamar al método de interfaz, o ejecutar la expresión lambda o desencadenar el evento definido para digerir el contenido del mensaje.

Algo así como a continuación:

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!");
    }
 }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top