问题描述
在将文本添加到 RichTextBox 中的 RTF 属性之前,我对文本进行了一些处理,添加了转义字符,然后将数据划分为多行.
before adding the text to RTF property in RichTextBox, i make some processing on the text, add escape character then divide the data to multiline.
文字是
line1 u001aline2 u001aline3 u001aline4
line1 u001aline2 u001aline3 u001aline4
当我将 VS 2015 与 .Net 4.6.2 一起使用时RTF 属性中的值为
When i use VS 2015 with .Net 4.6.2 the value in RTF property is
{\rtf1\fbidis\ansi\deff0{\fonttbl{\f0\fnil\fcharset0
Arial;}}
\viewkind4\uc1\pard\ltrpar\lang3073\fs24 Line1
\par
\v\'1a\v0 Line2
\par
\v\'1a\v0 Line3
\par
\v\'1a\v0 Line4\par
}
但是当我使用 .Net 4.7.1 切换到 VS 2017 时值改为
but when i switched to VS 2017 with .Net 4.7.1 the value changed to
{\rtf1\fbidis\ansi\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0
Arial;}}
{\*\generator Riched20 10.0.16299}\viewkind4\uc1
\pard\ltrpar\fs24\lang1033 Line1
\par
\v\'1a\v0 Line2 \par
\v\'1a\v0
Line3 \par
\pard\ltrpar\v\'1a\v0
Line4\par
}
当我访问 Text 属性时,我得到了这个 Text
and when i accessed the Text property i got this Text
Line1 Line2 Line3 Line4
Line1 Line2 Line3 Line4
RichTextBox 在 VS 2017 中删除了转义字符,有人知道为什么吗?
RichTextBox removes the escape character in VS 2017 anyone knows Why?
推荐答案
当您的应用程序面向 .NET 4.6.2(或更低版本)时,RichTextBox 会实例化 RichEdit 控件版本 3(版本描述 此处),当您的应用程序重定向到 .NET 4.7 时.1,它实例化 Rich Edit 版本 4.1 (msftedit.dll).RTF 表示的差异很可能是由较新版本的控件引起的.即使您的应用程序面向 4.7 及更高版本,您也可以通过在 AppContextSwitchOverrides 标记下添加具有以下兼容性开关的 app.config 文件来选择退出使用较新版本的 Rich Edit:
When your application targets .NET 4.6.2 (or below), RichTextBox instantiates RichEdit control version3 (versions are described here), when your application is retargeted to .NET 4.7.1, it instantiates Rich Edit version 4.1 (msftedit.dll). Difference in RTF representation is most likely caused by the newer version of the control. You can opt-out from using the newer version of Rich Edit even when your application targets 4.7 and above by adding app.config file with the following compatibility switch under AppContextSwitchOverrides tag:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/>
</startup>
<runtime>
<AppContextSwitchOverrides
value="Switch.System.Windows.Forms.DoNotLoadLatestRichEditControl=true" />
</runtime>
</configuration>
相反,您可以通过将上述 AppContextSwitch 设置为 false,在面向 .NET 4.6.2 的应用程序中加载 Rich Edit 4.1.
Conversely you can load Rich Edit 4.1 in an application that targets .NET 4.6.2 by setting the above AppContextSwitch to false.
这篇关于RichTextBox 从文本中删除转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!