3

如果我在 oob 应用程序中有两个窗口,我如何在它们之间进行通信?

这是 silverlight 5 的新功能,它允许多个窗口。

4

2 回答 2

3

选项 1:MVVM 模式

两个窗口共享对同一视图模型的引用。一个人所做的更改被两个人看到。

选项 2:普通参考

窗口 A 在创建时可以如何引用 Windows B。

选项 3:消息传递

您可以在 Load 事件中订阅一个全局事件。(确保在 Unload 事件中取消订阅,否则会泄漏内存!)Windows 可以向其他窗口侦听的该事件发布消息。

于 2011-10-11T20:00:50.380 回答
3

它们在一个通用应用程序中运行。因此它们共享相同的静态数据。因此通信选择的范围非常大。这是一个例子:-

public class MessageEventArgs : EventArgs
{
      public MessageEventArgs(object payload)
      {
           Payload = payload;
      }

      public object Payload {get; private set; }
}

public class Messenger
{
    private static readonly Messenger _current = new Messenger();
    public static Messenger Current { get { return _current; } }

    public event EventHandler<MessageEventArgs> MessageReceived;

    public void Send(object payload)
    {
          if (MessageReceived != null)
               MessageReceived(this, new MessageEventArgs(payload));
    }
}

All windows can attach a handler to Messenger.Current.MessageReceived (just be sure to detach when the window closes) and any window can call Messenger.Current.Send.

Ok so you wouldn't actually use this code its a bit rubbish, the point is Windows in SL5 are not isolated. You can create whatever internal application communication mechanism you need.

于 2011-10-11T20:02:17.773 回答