如何从 wpf 中的方法调用返回格式化文本 [英] How to return formatted text from a method call in wpf
问题描述
我正在开发一个插件界面,我希望有一种方法可以以一种 WPF 控件可以只显示插件结果的方式返回格式化文本.返回该文本的最佳方式是什么?
I'm developing a plugin interface and I would like to have one method that returns formatted text in a way that a WPF control can just show the plugin result. What would be the best way to return that text?
这样我只需要将插件结果分配给 WPF 文本控件(可能是 RichTextBox 或类似的东西).
This way I will only need to assign the plugin result to a WPF text control (maybe a RichTextBox or something like that).
我想支持粗体、下划线等...
I would like to support bold, underline, etc ...
总而言之,我正在从字符串格式"中进行搜索,该字符串允许第 3 方编码人员返回一个带有编码格式的简单字符串,因此我只需要执行以下操作:
Summarizing, I'm searching from a string "format" that allow 3rd party coders to return me a simple string with encoded format so I just need to do:
myRichTextBox.Text = (IPlugin)3rdPartyPlugin.ExecutePlugin();
文本显示格式.
推荐答案
返回格式化文本的方法背后有多种代码......
There are various Code behind ways to return a formatted text...
1] 作为文本
2] 作为图形呈现的文本...
2] As a graphically rendered text ...
在任何自定义控件的重写 OnRender() 方法中,您可以以图形方式将格式化文本绘制为绘图...
In any custom control's overriden OnRender() method you can graphically paint the formatted text as a drawing ...
protected override void OnRender(DrawingContext drawingContext)
{
this.formattedToolTip = new FormattedText(
(string)this.TextProperty,
System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(
new FontFamily("Arial"),
FontStyles.Normal,
FontWeights.Bold,
FontStretches.Normal),
11,
new SolidColorBrush(Colors.Black));
drawingContext.DrawText(
this.formattedToolTip,
new Point(10, 10)); //// Margin of 10 pixels from top and left.
}
这篇关于如何从 wpf 中的方法调用返回格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!