在用户输入期间将文本转换为带有微笑的文本
我尝试解决这个问题。我有可绑定的文本和文档属性的 richTextBox,该控件派生自扩展 WPF 工具包的 richTextBox。
我的场景是:用户正在向 richTextBox 输入一些文本,如果他写了文本表情符号,我想将此文本表情符号转换为图像表情符号并在 richTextBox 中显示他。
例如:Hello world :) ->你好世界 IMAGE_SMILE
我有自己的类,它将带有表情符号的字符串转换为带有文本和图像的段落类型。
如下:
public Paragraph ConvertToMessageWithEmoticons(string rpText){...}
在 WPF 应用程序中,我将属性绑定到 richTextBox 控件的 Text 和 Document 属性上。
//bind on Text property of richTextBoxControl
public string RtbText
{
get { return _rtbText; }
set
{
_rtbText = value;
NotifyPropertyChanged("RtbText");
}
}
//bind on Document property of richTextBoxControl
public FlowDocument RtbFlowDocument
{
get { return _rtbFlowDocument; }
set
{
_rtbFlowDocument = value;
NotifyPropertyChanged("RtbFlowDocument");
}
}
我使用 Reactive Extensions for .NET (Rx) 并在属性 RtbText 上创建观察者
Observable.FromEvent<PropertyChangedEventArgs>(this, "PropertyChanged")
.Where(e => e.EventArgs.PropertyName == "RtbText")
.Select(_ => this.RtbText)
.Where(text => text.Length > 1)
.Do(AddSmiles)
.Throttle(TimeSpan.FromSeconds(500))
.Subscribe(AddSmiles);
我的问题是如何优雅地将带有微笑的文本替换为带有表情符号的文本。我尝试这样做:
private void AddSmiles(string text)
{
Paragraph paragraph = _smileConverter.ConvertToMessageWithEmoticons(RtbText);
RtbFlowDocument.Blocks.Clear();
RtbFlowDocument.Blocks.Add(paragraph);
}
我尝试仅检查 RtbText 的最后 3 个字符,但如果用户键入属性 RtbText 仍然会发生变化。
有什么建议如何在用户输入时将文本替换为带有微笑的文本吗?
I try solve this problem. I have bindable richTextBox for Text and Document properties, this control is derived from richTextBox from Extended WPF Toolkit.
My scenario is : User is typing some text to richTextBox, if he wrote text emoticons I want convert this text emoticons to image emoticons and show him in richTextBox.
For example: Hello world :) -> Hello world IMAGE_SMILE
I have own class which convert string with emoticons to type of Paragraph with text and images.
Here ist it:
public Paragraph ConvertToMessageWithEmoticons(string rpText){...}
In WPF app I bind properties on Text and Document properties of richTextBox control.
//bind on Text property of richTextBoxControl
public string RtbText
{
get { return _rtbText; }
set
{
_rtbText = value;
NotifyPropertyChanged("RtbText");
}
}
//bind on Document property of richTextBoxControl
public FlowDocument RtbFlowDocument
{
get { return _rtbFlowDocument; }
set
{
_rtbFlowDocument = value;
NotifyPropertyChanged("RtbFlowDocument");
}
}
I use Reactive Extensions for .NET (Rx) and make Observer on property RtbText
Observable.FromEvent<PropertyChangedEventArgs>(this, "PropertyChanged")
.Where(e => e.EventArgs.PropertyName == "RtbText")
.Select(_ => this.RtbText)
.Where(text => text.Length > 1)
.Do(AddSmiles)
.Throttle(TimeSpan.FromSeconds(500))
.Subscribe(AddSmiles);
My problem is how elegant replace text with smiles to text with emoticons. I try this:
private void AddSmiles(string text)
{
Paragraph paragraph = _smileConverter.ConvertToMessageWithEmoticons(RtbText);
RtbFlowDocument.Blocks.Clear();
RtbFlowDocument.Blocks.Add(paragraph);
}
I try check on smiles only 3 last characters of RtbText, but if is user typing property RtbText is still change.
Any advice how replace text to text with smile during user typing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前已经实现过这样的类(尽管我会分享,但闭源)。棘手的部分是当用户按退格键时管理插入符号的位置 - 因为然后您需要将笑脸转换回
:-
,同时处理边缘情况(段落的开头/结尾、文档, ETC)I've implemented classes like this before (closed source though otherwise I'd share). The tricky part is managing the position of the caret when the user hits backspace - cause then you need to convert the Smiley back to the
:-
, while handling the edge cases (beginning/end of paragraph, document, etc)如果您获得了插入符位置,则可以在文本更改时查找插入符周围的字符(前 3 个和后 3 个)。
If you get the carret position you can look for the characters around the carret (the previous 3 and the next 3) when the text changes.