【问题标题】:Sending Generic Messages发送通用消息
【发布时间】:2011-05-28 04:38:30
【问题描述】:
public class Foo<T> where T: Entity
{}

public class Foo1
: Foo<Telephone>
{
}

public class Foo2
: Foo<Home>
{   
}

如何将 Foo1 发送到 Foo2?我意识到消息是键入的,因此收到了相同类型的消息 - 但我需要在派生类之间发送消息......

非常感谢一个例子。

【问题讨论】:

    标签: mvvm-light


    【解决方案1】:

    另一种方法是创建您自己的类,其中包含您希望交付的有效负载(Foo1 或简单的object)。然后在Foo2注册接收你刚刚创建的类的消息。

    此链接通过一个非常易于理解的示例说明了如何进行。

    MVVM Light Toolkit Soup To Nuts 3 - Jesse Liberty

    【讨论】:

      【解决方案2】:

      理论上,mvvmlight 中的消息传递应该是一劳永逸的......发送者不关心谁收到消息,接收者也不关心谁发送消息,只要它正在侦听的正确类型。 我通过多次尝试和错误发现,制作自己的消息比使用 mvvm-light 提供的默认消息要容易得多,它们是一个很好的起点,但有时你会发现自己在跳槽..

      public class ExceptionMessage : GalaSoft.MvvmLight.Messaging.GenericMessage<System.Exception>
          {
              public ExceptionMessage(System.Exception content) : base(content) { }
              public ExceptionMessage(object sender, System.Exception content) : base(sender, content) { }
              public ExceptionMessage(object sender, object target, System.Exception content) : base(sender, target, content) { }
          }
      

      收货人代码:

      Messenger.Default.Register<Core.Messaging.ExceptionMessage>(this, ex => ShowExceptionMessage(ex));
      

      发件人代码:

      public void LogException(Exception content)
              {
                  _messenger.Send<Core.Messaging.ExceptionMessage>(new ExceptionMessage(content));
                  //GetBw().RunWorkerAsync(content);
                  WriteToDatabaseLog(content);
              }
      

      是的,这确实违反了我第一句话中的建议,但理论上我可以拥有多个 vm 或查看监听异常消息。

      也许另一个例子可以帮助你...我讨厌整个 foo 的事情...它总是让我感到困惑...

      这是我的核心模块:

      public class SaveNotification<T> : GalaSoft.MvvmLight.Messaging.NotificationMessage<T> where T : GalaSoft.MvvmLight.ViewModelBase
          {
              public SaveNotification(T content, string notification) : base(content, notification) { }
              public SaveNotification(object sender, T content, string notification) : base(sender, content, notification) { }
              public SaveNotification(object sender, object target, T content, string notification) : base(sender, target, content, notification) { }
          }
      

      这是我在我的虚拟机中使用它的方式:

      public void OnSubmitChanges(SubmitOperation so)
              {
                  if (so.HasError)
                  {
                      Infrastructure.GetService<IExceptionLoggingInterface>().LogException(this, so.Error);
                  }
                  else
                  {
                      //Save Responses
                      _messenger.Send<Messages.NavigationRequest<SubClasses.URI.PageURI>>(GetNavRequest_HOME());
                      ClearQuestionaire(true);
                      _messenger.Send<WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel>>(GetSuccessfulSaveNotification());
      
                  }
      
                  Wait.End();
              }
      
              private WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel> GetSuccessfulSaveNotification()
              {
                  return new WavelengthIS.Core.Messaging.SaveNotification<QuestionairreViewModel>(this, "Save Successfull");
              }
      

      【讨论】:

      • 我将此标记为答案,因为我确实想找到解决问题的方法,但我从未见过 GenericMessage 的示例。尽管如此,我认为这不会解决我的问题。在我的示例中,Foo1 和 Foo2 实际上是两种不同的类型——因为这些类型是在编译时(而不是运行时)定义的。我知道这一点,但是当我被困在这个问题上时并没有想到它(我猜键盘上的时间太多了!)我如何解决我的问题是通过从基类中提取的接口。使用接口创建的消息 - 为所有人提供通用接口。
      猜你喜欢
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多