【WPF】实现类似QQ聊天消息的界面
最近公司有个项目,是要求实现类似 QQ 聊天这种功能的。
如下图

这没啥难的,稍微复杂的也就表情的解析而已。
表情在传输过程中的实现参考了新浪微博,采用半角中括号代表表情的方式。例如:“abc[doge]def”就会显示 abc,然后一个
,再 def。
于是动手就干。
创建一个模板控件来进行封装,我就叫它 ChatMessageControl,有一个属性 Text,表示消息内容。内部使用一个 TextBlock 来实现。
于是博主三下五除二就写出了以下代码:
C#
[TemplatePart(Name = TextBlockTemplateName, Type = typeof(TextBlock))]
public class ChatMessageControl : Control
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(nameof(Text), typeof(string), typeof(ChatMessageControl), new PropertyMetadata(default(string), OnTextChanged)); private const string TextBlockTemplateName = "PART_TextBlock"; private static readonly Dictionary<string, string> Emotions = new Dictionary<string, string>
{
["doge"] = "pack://application:,,,/WpfQQChat;component/Images/doge.png",
["喵喵"] = "pack://application:,,,/WpfQQChat;component/Images/喵喵.png"
}; private TextBlock _textBlock; static ChatMessageControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ChatMessageControl), new FrameworkPropertyMetadata(typeof(ChatMessageControl)));
} public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
} public override void OnApplyTemplate()
{
_textBlock = (TextBlock)GetTemplateChild(TextBlockTemplateName); UpdateVisual();
} private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (ChatMessageControl)d; obj.UpdateVisual();
} private void UpdateVisual()
{
if (_textBlock == null)
{
return;
} _textBlock.Inlines.Clear(); var buffer = new StringBuilder();
foreach (var c in Text)
{
switch (c)
{
case '[':
_textBlock.Inlines.Add(buffer.ToString());
buffer.Clear();
buffer.Append(c);
break; case ']':
var current = buffer.ToString();
if (current.StartsWith("["))
{
var emotionName = current.Substring();
if (Emotions.ContainsKey(emotionName))
{
var image = new Image
{
Width = ,
Height = ,
Source = new BitmapImage(new Uri(Emotions[emotionName]))
};
_textBlock.Inlines.Add(new InlineUIContainer(image)); buffer.Clear();
continue;
}
} buffer.Append(c);
_textBlock.Inlines.Add(buffer.ToString());
buffer.Clear();
break; default:
buffer.Append(c);
break;
}
} _textBlock.Inlines.Add(buffer.ToString());
}
}
因为这篇博文只是个演示,这里博主就只放两个表情好了,并且耦合在这个控件里。
XAML
<Style TargetType="local:ChatMessageControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ChatMessageControl">
<TextBlock x:Name="PART_TextBlock"
TextWrapping="Wrap" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
没啥好说的,就是包了一层而已。
效果:

自我感觉良好,于是乎博主就提交代码,发了个版本到测试环境了。
但是,第二天,测试却给博主提了个 bug。消息无法选择、复制。

在 UWP 里,TextBlock 控件是有 IsTextSelectionEnabled 属性的,然而 WPF 并没有。这下头大了,于是博主去查了一下 StackOverflow,大佬们回答都是说用一个 IsReadOnly 为 True 的 TextBox 来实现。因为我这里包含了表情,所以用 RichTextBox 来实现吧。不管行不行,先试试再说。
在原来的代码上修改一下,反正表情解析一样的,但这里博主为了方便写 blog,就新开一个控件好了。
C#
[TemplatePart(Name = RichTextBoxTemplateName, Type = typeof(RichTextBox))]
public class ChatMessageControlV2 : Control
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(nameof(Text), typeof(string), typeof(ChatMessageControlV2), new PropertyMetadata(default(string), OnTextChanged)); private const string RichTextBoxTemplateName = "PART_RichTextBox"; private static readonly Dictionary<string, string> Emotions = new Dictionary<string, string>
{
["doge"] = "pack://application:,,,/WpfQQChat;component/Images/doge.png",
["喵喵"] = "pack://application:,,,/WpfQQChat;component/Images/喵喵.png"
}; private RichTextBox _richTextBox; static ChatMessageControlV2()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ChatMessageControlV2), new FrameworkPropertyMetadata(typeof(ChatMessageControlV2)));
} public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
} public override void OnApplyTemplate()
{
_richTextBox = (RichTextBox)GetTemplateChild(RichTextBoxTemplateName); UpdateVisual();
} private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (ChatMessageControlV2)d; obj.UpdateVisual();
} private void UpdateVisual()
{
if (_richTextBox == null)
{
return;
} _richTextBox.Document.Blocks.Clear(); var paragraph = new Paragraph(); var buffer = new StringBuilder();
foreach (var c in Text)
{
switch (c)
{
case '[':
paragraph.Inlines.Add(buffer.ToString());
buffer.Clear();
buffer.Append(c);
break; case ']':
var current = buffer.ToString();
if (current.StartsWith("["))
{
var emotionName = current.Substring();
if (Emotions.ContainsKey(emotionName))
{
var image = new Image
{
Width = ,
Height = ,
Source = new BitmapImage(new Uri(Emotions[emotionName]))
};
paragraph.Inlines.Add(new InlineUIContainer(image)); buffer.Clear();
continue;
}
} buffer.Append(c);
paragraph.Inlines.Add(buffer.ToString());
buffer.Clear();
break; default:
buffer.Append(c); break;
}
} paragraph.Inlines.Add(buffer.ToString()); _richTextBox.Document.Blocks.Add(paragraph);
}
}
XAML
<Style TargetType="local:ChatMessageControlV2">
<Setter Property="Foreground"
Value="Black" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ChatMessageControlV2">
<RichTextBox x:Name="PART_RichTextBox"
MinHeight="0"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Foreground="{TemplateBinding Foreground}"
IsReadOnly="True">
<RichTextBox.Resources>
<ResourceDictionary>
<Style TargetType="Paragraph">
<Setter Property="Margin"
Value="0" />
<Setter Property="Padding"
Value="0" />
<Setter Property="TextIndent"
Value="0" />
</Style>
</ResourceDictionary>
</RichTextBox.Resources>
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Copy"
Header="复制" />
</ContextMenu>
</RichTextBox.ContextMenu>
</RichTextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
XAML 稍微复杂一点,因为我们需要让一个文本框高仿成一个文字显示控件。
感觉应该还行,然后跑起来之后

复制是能复制了,然而我的布局呢?

因为一时间也没想到解决办法,于是博主只能回滚代码,把 bug 先晾在那里了。
经过了几天上班带薪拉屎之后,有一天博主在厕所间玩着宝石连连消的时候突然灵光一闪。对于 TextBlock 来说,只是不能选择而已,布局是没问题的。对于 RichTextBox 来说,布局不正确是由于 WPF 在测量与布局的过程中给它分配了无限大的宽度。那么,能不能将两者结合起来,TextBlock 做布局,RichTextBox 做功能呢?想到这里,博主关掉了宝石连连消,擦上屁股,开始干活。
C#
[TemplatePart(Name = TextBlockTemplateName, Type = typeof(TextBlock))]
[TemplatePart(Name = RichTextBoxTemplateName, Type = typeof(RichTextBox))]
public class ChatMessageControlV3 : Control
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(nameof(Text), typeof(string), typeof(ChatMessageControlV3), new PropertyMetadata(default(string), OnTextChanged)); private const string RichTextBoxTemplateName = "PART_RichTextBox";
private const string TextBlockTemplateName = "PART_TextBlock"; private static readonly Dictionary<string, string> Emotions = new Dictionary<string, string>
{
["doge"] = "pack://application:,,,/WpfQQChat;component/Images/doge.png",
["喵喵"] = "pack://application:,,,/WpfQQChat;component/Images/喵喵.png"
}; private RichTextBox _richTextBox;
private TextBlock _textBlock; static ChatMessageControlV3()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ChatMessageControlV3), new FrameworkPropertyMetadata(typeof(ChatMessageControlV3)));
} public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
} public override void OnApplyTemplate()
{
_textBlock = (TextBlock)GetTemplateChild(TextBlockTemplateName);
_richTextBox = (RichTextBox)GetTemplateChild(RichTextBoxTemplateName); UpdateVisual();
} private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (ChatMessageControlV3)d; obj.UpdateVisual();
} private void UpdateVisual()
{
if (_textBlock == null || _richTextBox == null)
{
return;
} _textBlock.Inlines.Clear();
_richTextBox.Document.Blocks.Clear(); var paragraph = new Paragraph(); var buffer = new StringBuilder();
foreach (var c in Text)
{
switch (c)
{
case '[':
_textBlock.Inlines.Add(buffer.ToString());
paragraph.Inlines.Add(buffer.ToString());
buffer.Clear();
buffer.Append(c);
break; case ']':
var current = buffer.ToString();
if (current.StartsWith("["))
{
var emotionName = current.Substring();
if (Emotions.ContainsKey(emotionName))
{
{
var image = new Image
{
Width = ,
Height =
};// 占位图像不需要加载 Source 了
_textBlock.Inlines.Add(new InlineUIContainer(image));
}
{
var image = new Image
{
Width = ,
Height = ,
Source = new BitmapImage(new Uri(Emotions[emotionName]))
};
paragraph.Inlines.Add(new InlineUIContainer(image));
} buffer.Clear();
continue;
}
} buffer.Append(c);
_textBlock.Inlines.Add(buffer.ToString());
paragraph.Inlines.Add(buffer.ToString());
buffer.Clear();
break; default:
buffer.Append(c);
break;
}
} _textBlock.Inlines.Add(buffer.ToString());
paragraph.Inlines.Add(buffer.ToString()); _richTextBox.Document.Blocks.Add(paragraph);
}
}
C# 代码相当于把两者结合起来而已。
XAML
<Style TargetType="local:ChatMessageControlV3">
<Setter Property="Foreground"
Value="Black" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:ChatMessageControlV3">
<Grid>
<TextBlock x:Name="PART_TextBlock"
Padding="6,0,6,0"
IsHitTestVisible="False"
Opacity="0"
TextWrapping="Wrap" />
<RichTextBox x:Name="PART_RichTextBox"
Width="{Binding ElementName=PART_TextBlock, Path=ActualWidth}"
MinHeight="0"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Foreground="{TemplateBinding Foreground}"
IsReadOnly="True">
<RichTextBox.Resources>
<ResourceDictionary>
<Style TargetType="Paragraph">
<Setter Property="Margin"
Value="0" />
<Setter Property="Padding"
Value="0" />
<Setter Property="TextIndent"
Value="0" />
</Style>
</ResourceDictionary>
</RichTextBox.Resources>
<RichTextBox.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Copy"
Header="复制" />
</ContextMenu>
</RichTextBox.ContextMenu>
</RichTextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
XAML 大体也是将两者结合起来,但是把 TextBlock 设置为隐藏(但占用布局),而 RichTextBox 则绑定 TextBlock 的宽度。
至于为啥 TextBlock 有一个左右边距为 6 的 Padding 嘛。在运行之后,博主发现,RichTextBox 的内容会离左右有一定的距离,但是没找到相关的属性能够设置,如果正在看这篇博文的你,知道相关的属性的话,可以在评论区回复一下,博主我将会万分感激。
最后是我们的效果啦。

最后,因为现在 WPF 是开源(https://github.com/dotnet/wpf)的了,因此已经蛋疼不已的博主果断提了一个 issue(https://github.com/dotnet/wpf/issues/307),希望有遇到同样困难的小伙伴能在上面支持一下,让巨硬早日把 TextBlock 选择这功能加上。
【WPF】实现类似QQ聊天消息的界面的更多相关文章
- iOS开发学习-类似微信聊天消息中的电话号码点击保存到通讯录中的功能
类似微信聊天消息中的电话号码点击保存到通讯录中的功能,ABAddress的实现在iOS9中是不能正常使用的,点击完成后,手机会非常的卡,iOS9之后需要使用Contact新提供的方法来实现该功能.快捷 ...
- reactnative实现qq聊天消息气泡拖拽消失效果
前言(可跳过) 我在开发自己的APP时遇到了一个类似于qq聊天消息气泡拖拽消息的需求,因为在网上没有找到相关的组件,所以自己动手实现了一下 需求:对聊天消息气泡拖拽到一定长度松开时该气泡会消失(可自行 ...
- C#+ html 实现类似QQ聊天界面的气泡效果
/**定义两个人的头像*/ Myhead = "<img src=qrc:/chatdemo/Msg/Head.png width='30px'heigth='30px'>&qu ...
- 类似QQ在线离线好友界面
把头像设置成圆形的代码如下: package com.example.lesson6_11_id19; import android.content.Context; import android.c ...
- Android—简单的仿QQ聊天界面
最近仿照QQ聊天做了一个类似界面,先看下界面组成(画面不太美凑合凑合呗,,,,):
- WPF仿QQ聊天框表情文字混排实现
原文:WPF仿QQ聊天框表情文字混排实现 二话不说.先上图 图中分别有文件.文本+表情.纯文本的展示,对于同一个list不同的展示形式,很明显,应该用多个DataTemplate,那么也就需要Data ...
- 搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)
搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating ...
- 即时通信系统中如何实现:聊天消息加密,让通信更安全? 【低调赠送:QQ高仿版GG 4.5 最新源码】
加密重要的通信消息,是一个常见的需求.在一些政府部门的即时通信软件中(如税务系统),对聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新的GG 4.5中,增加了对聊天消息 ...
- Objective-c——UI基础开发第八天(QQ聊天界面)
一.知识点: QQ聊天界面 双模型的使用(dataModel和frameModel) UITextField的使用 通知的使用 拉伸图片的两种方法(slicing/image对象的resizeable ...
随机推荐
- 27.Socket,TCP,UDP,HTTP基本通信原理
Socket,TCP,UDP,HTTP基本通信原理(摘自百度): TCP.UDP,HTTP 底层通信都是通过 socket 套接字实现 网络上不同的计算机,也可以通信,那么就得使用网络套接字(sock ...
- 活用RPM获取包的信息
rpm -q 功效大 如果你想要在系统上安装.卸载或是升级软件,需要对系统软件进行查询:或是有如下的场景: 安装了一个软件,需要知道这个软件的版本. 遇到一个文件,不认识它,需要知道它是什么软件,有什 ...
- python入门(一):进入python的交互模式、pip的使用和数据类型
环境安装: https://www.python.org/ pycharm 社区版路径: http://www.jetbrains.com/pycharm/download/download-than ...
- 20175126《Java程序设计》第一周学习总结
# 学号 20175126 <Java程序设计>第一周学习总结 ## 教材学习内容总结 - 1.安装了WINDOS系统的JDK,并学会了利用JDK编写并编译JAVA程序的基本方法. ...
- FortiGate安全策略说明
1.安全策略原理 1)为了对数据流进行统一控制,方便用户配置和管理,FGT设备引入了安全策略的概念.通过配置安全策略,防火墙能够对经过设备的数据流进行有效的控制和管理. 2)当防火墙收到数据报文时,把 ...
- 322. Coin Change选取最少的硬币凑整-背包问题变形
[抄题]: You are given coins of different denominations and a total amount of money amount. Write a fun ...
- ES6 proxy(代理拦截) &&Reflect
- node.js中使用socket.io + express进行实时消息推送
socket.io是一个websocket库,包含客户端的js和服务端的node.js,可以在不同浏览器和移动设备上构建实时应用. 一.安装 socket.io npm install socket. ...
- (百度)centos7上安装apache指南
https://jingyan.baidu.com/album/c843ea0bb5ff3977931e4a14.html?picindex=1 原文就不拷贝了.留个网址
- NC 6系总账凭证联查原始单据
单据联查凭证可以找个如收款结算单的仿写一个. 而总账凭证联查单据则需要实现联查单据的类,重新写一个类.并把类注册进数据库. 最终效果: public class QuerySellcarryBillS ...