WPF implements message center

WPF implements message center

This article will explain the functions of implementing a message center based on WPF. For example, common software will often receive messages such as "news" and "announcements" pushed by the server.

最后更新 5/11/2022 7:31 AM
juster.zhu
预计阅读 4 分钟
分类
WPF
标签
.NET WPF

1. Summary

This article will explain the functions of implementing a message center based on WPF. For example, common software will often receive messages such as "news" and "announcements" pushed by the server. At this time, this requirement needs to be analyzed.

Functional analysis is as follows:

  • The message content is displayed.
  • Message management add, delete, and batch delete.
  • Message classification (notification type messages, interaction type messages, for example, can jump to a certain connection or module within a program)
  • Message processing (accept, delete, ignore)

2. Realization

  1. Message content display The custom control considered here is a Listbox, and the message itself is the content of multiple items and requires manipulation of each item.
  <ListBox
    Grid.Row="1"
    MaxHeight="335"
    Background="{x:Null}"
    BorderThickness="0"
    ItemContainerStyle="{DynamicResource ListBoxItemStyle}"
    ItemsSource="{Binding MessageItem}"
    ScrollViewer.HorizontalScrollBarVisibility="Hidden">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate DataType="{x:Type localModel:MessageItemModel}">
            <Border
                Height="30"
                BorderBrush="#FFBDBDBD"
                BorderThickness="0,0,0,0.6">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="40" />
                    </Grid.ColumnDefinitions>
                    <DockPanel>
                        <Label
                            MaxWidth="70"
                            Content="{Binding Path=Name}"
                            Foreground="Red"
                            ToolTip="{Binding Path=Name}" />
                        <Label
                            MaxWidth="130"
                            Content="{Binding Path=Content}"
                            Foreground="White"
                            ToolTip="{Binding Path=Content}" />
                    </DockPanel>
                    <CheckBox
                        Grid.Column="1"
                        FlowDirection="RightToLeft"
                        IsChecked="{Binding Path=CheckBoxState}" />
                </Grid>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
  1. Message management add, delete, and batch delete

After the main container is determined, each next message can be a custom ListboxItem, and specific processing must be carried out for each message.

For example, for a notification type message, only the OK button is needed.

Interactive type messages need to be confirmed, deleted, and ignored

 <DataTemplate x:Key="SelectedTemplate" DataType="{x:Type localModel:MessageItemModel}">
     <Border BorderBrush="#FFBDBDBD" BorderThickness="0,0,0,0.6">
         <StackPanel>
             <TextBox
                 MaxWidth="240"
                 MaxHeight="200"
                 Padding="0,5,0,0"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 Background="Transparent"
                 BorderThickness="0"
                 FontSize="14"
                 Foreground="White"
                 IsReadOnly="True"
                 Text="{Binding Path=Content}"
                 TextAlignment="Center"
                 TextWrapping="WrapWithOverflow"
                 ToolTip="{Binding Path=Content}"
                 VerticalScrollBarVisibility="Auto" />
             <StackPanel
                 Margin="5,5,5,9"
                 HorizontalAlignment="Center"
                 VerticalAlignment="Center"
                 Orientation="Horizontal">
                 <Button
                     Width="60"
                     Height="25"
                     Margin="5"
                     Command="{Binding DataContext.ClickAcceptCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
                     CommandParameter="{Binding}"
                     Content="接受"
                     Style="{StaticResource BlueButtonStyle}"
                     Visibility="{Binding Path=InvitationType, Converter={StaticResource BooleanToVisibilityConverter}}" />
                 <Button
                     Width="60"
                     Height="25"
                     Margin="5"
                     Command="{Binding DataContext.ClickRefuseCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
                     CommandParameter="{Binding}"
                     Content="忽略"
                     Style="{StaticResource BlueButtonStyle}"
                     Visibility="{Binding Path=InvitationType, Converter={StaticResource BooleanToVisibilityConverter}}" />
                 <Button
                     Width="60"
                     Height="25"
                     Margin="5"
                     Command="{Binding DataContext.ClickAgreeCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
                     CommandParameter="{Binding}"
                     Content="确认"
                     Style="{StaticResource BlueButtonStyle}"
                     Visibility="{Binding Path=NoticeType, Converter={StaticResource BooleanToVisibilityConverter}}" />
             </StackPanel>
         </StackPanel>
     </Border>
 </DataTemplate>
  1. message classification

Here, you just need to define specific enumerations at the Model layer.

/// <summary>
/// 消息处理结果
/// </summary>
public enum SysMessageResult
{
    /// <summary>
    /// 未处理
    /// </summary>
    Unhandled = 0,
    /// <summary>
    /// 同意
    /// </summary>
    Processed = 1
}

/// <summary>
/// 消息类型
/// </summary>
public enum SysMessageType
{
    /// <summary>
    /// 通知类型
    /// </summary>
    NoticeType = 0,
    /// <summary>
    /// 其他类型
    /// </summary>
    OtherType = 1
}
  1. message processing

Message processing refers to the logic used by the three buttons of "OK","Accept" and "Ignore" to process message content. If your partner needs it, you can modify it according to your own needs. I define it here as follows:

  • Confirm: Notification messages are usually processed, and processing simply removes the item from the message list and does no other action.
  • Accept: is a button that handles the interaction type that handles removing the item from the message list and triggers other business processing behaviors.
  • Ignore: Processes all types of messages, except that they are not displayed in the UI but will still exist in the message list to process the message next time or during idle time.
Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 9/13/2025

Migration from WPF to Avalonia series: Why I have to migrate WPF programs to Avalonia

In the past few years, our host computer software has been mainly developed using WPF and WinForm. These technologies are really easy to use on the Windows platform, and they have also accompanied us through the stage of small-scale trial production to today's large-scale delivery. However, with the development of business and changes in customer needs, the single Windows technology stack has gradually become a hurdle that we must overcome.

继续阅读
同分类 / 同标签 1/26/2025

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

继续阅读