阅读 42

WPF实现消息中心

一、概要

本文将讲解基于WPF实现一个消息中心的功能,比如常见的软件当中会经常收到服务端推送的“新闻”、“公告”等消息。这个时候就需要对这个需求进行分析了。

功能分析如下:

  • 消息内容显示。
  • 消息管理增、删、批量删除。
  • 消息分类(通知类消息、交互类型消息例如可跳转到某个连接或程序内的模块)
  • 消息处理(接受、删除、忽略)

二、实现

源码地址:

1.消息内容显示

这里考虑自定义的控件为Listbox,消息本身是一个多项的内容且需要操作每一项。

  1. Grid.Row="1"
  2. MaxHeight="335"
  3. Background="{x:Null}"
  4. BorderThickness="0"
  5. ItemContainerStyle="{DynamicResource ListBoxItemStyle}"
  6. ItemsSource="{Binding MessageItem}"
  7. ScrollViewer.HorizontalScrollBarVisibility="Hidden">
  8. Orientation="Vertical" />
  9. DataType="{x:Type localModel:MessageItemModel}">
  10. Height="30"
  11. BorderBrush="#FFBDBDBD"
  12. BorderThickness="0,0,0,0.6">
  13. Width="1*" />
  14. Width="40" />
  15. MaxWidth="70"
  16. Content="{Binding Path=Name}"
  17. Foreground="Red"
  18. ToolTip="{Binding Path=Name}" />
  19. MaxWidth="130"
  20. Content="{Binding Path=Content}"
  21. Foreground="White"
  22. ToolTip="{Binding Path=Content}" />
  23. Grid.Column="1"
  24. FlowDirection="RightToLeft"
  25. IsChecked="{Binding Path=CheckBoxState}" />

2.消息管理增、删、批量删除

主要容器定下来之后那么接下来每一项消息就是自定义ListboxItem即可,针对每一条消息要有具体的处理。

例如:

  1. 通知类消息,只需要确定按钮。
  2. 交互类型消息,需要确认、删除、忽略

     

    1. x:Key="SelectedTemplate" DataType="{x:Type localModel:MessageItemModel}">
    2. BorderBrush="#FFBDBDBD" BorderThickness="0,0,0,0.6">
    3. MaxWidth="240"
    4. MaxHeight="200"
    5. Padding="0,5,0,0"
    6. HorizontalAlignment="Center"
    7. VerticalAlignment="Center"
    8. Background="Transparent"
    9. BorderThickness="0"
    10. FontSize="14"
    11. Foreground="White"
    12. IsReadOnly="True"
    13. Text="{Binding Path=Content}"
    14. TextAlignment="Center"
    15. TextWrapping="WrapWithOverflow"
    16. ToolTip="{Binding Path=Content}"
    17. VerticalScrollBarVisibility="Auto" />
    18. Margin="5,5,5,9"
    19. HorizontalAlignment="Center"
    20. VerticalAlignment="Center"
    21. Orientation="Horizontal">
    22. Width="60"
    23. Height="25"
    24. Margin="5"
    25. Command="{Binding DataContext.ClickAcceptCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
    26. CommandParameter="{Binding}"
    27. Content="接受"
    28. Style="{StaticResource BlueButtonStyle}"
    29. Visibility="{Binding Path=InvitationType, Converter={StaticResource BooleanToVisibilityConverter}}" />
    30. Width="60"
    31. Height="25"
    32. Margin="5"
    33. Command="{Binding DataContext.ClickRefuseCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
    34. CommandParameter="{Binding}"
    35. Content="忽略"
    36. Style="{StaticResource BlueButtonStyle}"
    37. Visibility="{Binding Path=InvitationType, Converter={StaticResource BooleanToVisibilityConverter}}" />
    38. Width="60"
    39. Height="25"
    40. Margin="5"
    41. Command="{Binding DataContext.ClickAgreeCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
    42. CommandParameter="{Binding}"
    43. Content="确认"
    44. Style="{StaticResource BlueButtonStyle}"
    45. Visibility="{Binding Path=NoticeType, Converter={StaticResource BooleanToVisibilityConverter}}" />

3.消息分类

这里就是在Model层定义好具体的枚举即可。

  1. ///
  2. /// 消息处理结果
  3. ///
  4. public enum SysMessageResult
  5. {
  6. ///
  7. /// 未处理
  8. ///
  9. Unhandled = 0,
  10. ///
  11. /// 同意
  12. ///
  13. Processed = 1
  14. }
  15. ///
  16. /// 消息类型
  17. ///
  18. public enum SysMessageType
  19. {
  20. ///
  21. /// 通知类型
  22. ///
  23. NoticeType = 0,
  24. ///
  25. /// 其他类型
  26. ///
  27. OtherType = 1
  28. }

4.消息处理

消息处理指的是,“确定”、“接受”、“忽略”这三个按钮对消息内容处理的逻辑,如果小伙伴需要可根据自己的需要修改。 我这里定义如下:

  • 确定:通常处理通知消息,处理仅仅是从消息列表中移除该项不做其他行为。
  • 接受:是处理交互类型的按钮,处理从消息列表中移除该项且触发其他业务处理行为。
  • 忽略:处理所有类型消息,只是不显示在UI中但还会存在于消息列表中下次或空闲时间处理消息。

原文:https://www.cnblogs.com/justzhuzhu/p/14883374.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐