zoukankan      html  css  js  c++  java
  • 使用Messenger实现MVVM的对话框弹出

    MvvmFoudation有很多相当棒的mvvm功能实现,摘了一个Messenger放在了自己的mvvm框架里,用以实现MVVM模式的对话框弹出。

    具体实现方法如下

    • 首先在App中定义一个Messenger属性,用于全局的信息通知与接收。
    • 注册Messenger的接收事件,该实现我放在了MainWindow中。
     1         public const string MSG_Exception = "Exception occurred.";
     2         public const string MSG_MessageBox = "A message box should be showed.";
     3 
     4 
     5 
     6         private void RegisterExceptionMessage()
     7         {
     8             App.Messenger.Register(MSG_Exception, (Action<Exception>)(param =>
     9             {
    10                 ModernDialog.ShowMessage((string)param.Message, "Error", MessageBoxButton.OK);
    11                 App.Logger.Error(param);
    12             }));
    13         }
    14 
    15         private void RegisterMessageBoxMessage()
    16         {
    17             App.Messenger.Register(MSG_MessageBox, (Action<string>)(param =>
    18             {
    19                 ModernDialog.ShowMessage((string)param, "Info", MessageBoxButton.OK);
    20             }));
    21         }
    MainWindow

    ModernDialog是我用的一个WPF的UI库中的对话框,样式比较好看,具体用什么对话框看心情~

    接收事件注册完了,就可以在其他ViewModel中发信啦!

    例如:

    App.Messenger.NotifyColleagues(MainWindow.MSG_MessageBox, "Hello world.");
    //弹对话框,显示指定的文本~
    App.Messenger.NotifyColleagues(MainWindow.MSG_Exception, ex);
    //这个用来发送异常

    显然这个Messenger有更多的使用方法,还在研究中!

  • 相关阅读:
    信用卡:银联,VISA,MasterCard
    Syncthing vs BitTorrent Sync
    语言代码
    ATMEL精妙的IRQ中断处理过程
    CAN 总线通信控制芯片SJA1000 的读写
    ARM ® and Thumb ®-2 指令系统
    DeJaVu update history
    74系列的型号
    2007 Audi A4 INSTRUMENT CLUSTER WIRING DIAGRAM
    0-10岁儿童体重、身高参考值
  • 原文地址:https://www.cnblogs.com/zhuyc110/p/5198942.html
Copyright © 2011-2022 走看看