我有这段 RTF 文本:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}}
{\colortbl ;\red0\green0\blue0;\red255\green0\blue0;}
\viewkind4\uc1\pard\qc\cf1\fs16 test \b bold \cf2\b0\i italic\cf0\i0\fs17
\par }
如何将此文本设置到WPF RichTextBox中?
解决方案:
public void SetRTFText(string text)
{
MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(text));
this.mainRTB.Selection.Load(stream, DataFormats.Rtf);
}
Default
实际上是指Encoding.Default
,因此它是系统的默认 ANSI 代码页。要注意不要通过派生类访问静态成员,这经常会导致误解。 - Thomas Levesque