有没有办法强制 UWP RichEditBox 在用户键入时仅使用 UTF 编码?

Is there a way to force the UWP RichEditBox use only UTF encoding when the user types?

我正在尝试将 UWP RichEditBox 的内容转换为 HTML。

为此,我尝试使用 RtfPipe 库(https://github.com/erdomke/RtfPipe). From the looks of it, this library has a problem on UWP, due to the fact that not all encodings are defined on that target framework. (This is the error you get, if you are interested: ,但接受的答案似乎并不是所有平台上的最佳选择 - 我什至没有成功编译建议的修复程序,因此它可能不再有效)

现在,作为避免这种情况发生的一种方法,我想知道是否有一种方法可以强制控件在用户键入文本时始终使用 UWP 定义的 UTF 变体之一对数据进行编码. 因为,现在,当我输入它时,我会得到这样的东西:

{\rtf1\fbidis\ansi\ansicpg1253\deff0\nouicompat\deflang1032{
....
\pard\tx720\cf1\f0\fs23\lang1033

...使库抛出异常。

我想,如果我设法让它不使用 ASCII 代码页,事情会很棒。 不过,在查看了控件属性之后,我没有看到可以使用的东西。有什么办法可以实现吗?

This is the error you get, if you are interested: Encoding.GetEncoding can't work in UWP app

如您所述,将此包与 UWP 应用程序一起使用时会引发内部错误。 System.ArgumentException: 'Windows-1252' is not a supported encoding name,通过我这边的测试,当UpdateEncoding.

时,RtfSpec.cs的代码行public static readonly Encoding AnsiEncoding = Encoding.GetEncoding("Windows-1252");抛出

从错误详细信息来看,UWP 似乎不支持 Windows-1252,另请参阅 this similar thread. You could use UTF instead as you want, for example, have a change on the library with following then it will work (testing demo here)。

public static readonly Encoding AnsiEncoding = Encoding.UTF8;

I haven't even managed to make the suggested fix compile, so it might not be valid anymore

Encoding.RegisterProvider method should be work, but it only support UWP or .NET Framework 4.6, it does't support the Portable Class Library. The RtfPipe library you mentioned is Portable Class Library, so that you cannot use Encoding.RegisterProvider. Encoding.GetEncoding方法支持Portable Class Library,详情请查看两者分类的版本信息。

I guess, if I manage to make it not use ASCII code pages

RTF itself uses the ANSI, PC-8, Macintosh, or IBM PC character set to control the representation and formatting of a document, you may not able to change that. Consider to update the library to resolve the issue 用于 UWP。