示例:WPF中自定义MessageService应用DialogHost、Snackbar、NotifyIcon显示各种场景提示消息

时间:2023-03-09 08:23:32
示例:WPF中自定义MessageService应用DialogHost、Snackbar、NotifyIcon显示各种场景提示消息

原文:示例:WPF中自定义MessageService应用DialogHost、Snackbar、NotifyIcon显示各种场景提示消息

一、目的:不同交互场景需要提示不同的消息,不同的消息需要用不同的效果来展示,应用DialogHost(对话框)、NotifyIcon(消息提示)、Snackbar(气泡消息)显示各种场景提示消息,应用在ViewModel中

二、实现:

1、等待对话框

2、确定对话框

3、确定与取消对话框

4、百分比进度和文本进度对话框

5、气泡提示消息(NotifyIcon)

6、提示消息(Snackbar)

三、示例:

示例:WPF中自定义MessageService应用DialogHost、Snackbar、NotifyIcon显示各种场景提示消息

说明:

1、对话框:常规对话消息如上图,等待对话框、消息对话、进度对话框;

(目前只封装如上这几种,自定义对话框只需创建用户控件调用通用加载方法即可,后续更新...)

2、提示消息:当进度保存成功是需要一个提示消息,显示2s自动隐藏即可(如图中友情提示部分分) ;

3、气泡消息:当程序处于隐藏或某种状态时需要应用气泡提示消息;

四、代码:


  1. [ViewModel("Loyout")]
  2. class LoyoutViewModel : MvcViewModelBase
  3. {
  4. /// <summary> 命令通用方法 </summary>
  5. protected override async void RelayMethod(object obj)
  6. {
  7. string command = obj?.ToString();
  8. // Do:对话消息
  9. if (command == "Button.ShowDialogMessage")
  10. {
  11. await MessageService.ShowSumitMessge("这是消息对话框?");
  12. }
  13. // Do:等待消息
  14. else if (command == "Button.ShowWaittingMessge")
  15. {
  16. await MessageService.ShowWaittingMessge(() => Thread.Sleep(2000));
  17. }
  18. // Do:百分比进度对话框
  19. else if (command == "Button.ShowPercentProgress")
  20. {
  21. Action<IPercentProgress> action = l =>
  22. {
  23. for (int i = 0; i < 100; i++)
  24. {
  25. l.Value = i;
  26. Thread.Sleep(50);
  27. }
  28. Thread.Sleep(1000);
  29. MessageService.ShowSnackMessageWithNotice("加载完成!");
  30. };
  31. await MessageService.ShowPercentProgress(action);
  32. }
  33. // Do:文本进度对话框
  34. else if (command == "Button.ShowStringProgress")
  35. {
  36. Action<IStringProgress> action = l =>
  37. {
  38. for (int i = 1; i <= 100; i++)
  39. {
  40. l.MessageStr = $"正在提交当前页第{i}份数据,共100份";
  41. Thread.Sleep(50);
  42. }
  43. Thread.Sleep(1000);
  44. MessageService.ShowSnackMessageWithNotice("提交完成:成功100条,失败0条!");
  45. };
  46. await MessageService.ShowStringProgress(action);
  47. }
  48. // Do:确认取消对话框
  49. else if (command == "Button.ShowResultMessge")
  50. {
  51. Action<object, DialogClosingEventArgs> action = (l, k) =>
  52. {
  53. if ((bool)k.Parameter)
  54. {
  55. MessageService.ShowSnackMessageWithNotice("你点击了取消");
  56. }
  57. else
  58. {
  59. MessageService.ShowSnackMessageWithNotice("你点击了确定");
  60. }
  61. };
  62. MessageService.ShowResultMessge("确认要退出系统?", action);
  63. }
  64. // Do:提示消息
  65. else if (command == "Button.ShowSnackMessage")
  66. {
  67. MessageService.ShowSnackMessageWithNotice("这是提示消息?");
  68. }
  69. // Do:气泡消息
  70. else if (command == "Button.ShowNotifyMessage")
  71. {
  72. MessageService.ShowNotifyMessage("你有一条报警信息需要处理,请检查", "Notify By HeBianGu");
  73. }
  74. }
  75. }

下载地址:https://github.com/HeBianGu/WPF-ControlBase.git