質問

私はWP7に慣れていて、iPhone開発から来ています。 iPhoneでは、nsnotificationCenterを使用してプログラムに何かを通知することに慣れています。 nsnotificationCenterは、フレームワークに枠を超えてビルドインしています。 WP7に似たようなものはありますか? Uppon MVVM-Light Toolkitをつまずきましたが、それを正しく使用する方法がわかりません。

私がやりたいこと:

  • Notification-IDに登録し、通知IDが受信されたときに何かを行う
  • Notification-IDとコンテキスト(オブジェクトに渡すオブジェクト)で通知を送信します
  • 同じ通知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");
役に立ちましたか?

解決

MVVMライトツールキットを使用する方法は次のとおりです。

登録:

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

送信:

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

他のヒント

ここ http://www.silverlightshow.net/items/implementing-push-notifications-in-windows-phone-7.aspx Windows Phone 7でプッシュ通知を使用する方法についての優れた例を見つけることができます。

これによって送信された各メッセージについて、バシの要件に基づいて特定のインターフェイスを実装する、またはランバを呼び出すか、イベントをトリガーするシングルトンのリストを作成するシングルトンを作成することにより、NSNotificationCenterと同じ結果をアーカイブすることを確信していますSingleton ObservablesのリストとメッセージIDをチェックします。1つ以上が見つかったら、インターフェイスメソッドを呼び出すか、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!");
    }
 }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top