复制 UWP RichEditBox 内容会添加额外的行
Duplicating UWP RichEditBox content adds extra lines
我正在尝试在 ;RichEditBox 中获取和设置富文本,但每次执行 GetText 然后再执行 SetText 时,都会添加一个额外的回车符 return。
这是一个超级简单的示例,其中包含一个按钮,该按钮确实会被设置。试试看每次执行 Get-Set 时都会添加一个额外的马车 return。
XAML
<StackPanel>
<Button Content="Get-Set" Click="OnGetSet"/>
<RichEditBox x:Name="RichEditor" Width="300" Height="200"/>
</StackPanel>
C#
private void OnGetSet(object sender, RoutedEventArgs e)
{
RichEditor.Document.GetText(TextGetOptions.FormatRtf, out value);
RichEditor.Document.SetText(TextSetOptions.FormatRtf, value);
}
我在 SetText 和 GetText 中尝试了各种选项,但我能够阻止插入额外的回车 return。
有人有什么建议吗?
我最终找到了一个合理的解决方法。我正在获取完整的文本范围,然后在范围而不是文档上调用 GetText。
我不确定这是否是最佳解决方案,但它工作正常。
已更新 C#
private void OnGetSet(object sender, RoutedEventArgs e)
{
var value = GetText(RichEditor);
RichEditor.Document.SetText(TextSetOptions.FormatRtf, value);
}
public string GetText(RichEditBox editor)
{
// get the actual size of the text
editor.Document.GetText(TextGetOptions.UseLf, out string text);
// get the text in the total range - to avoid getting extra lines
var range = editor.Document.GetRange(0, text.Length);
range.GetText(TextGetOptions.FormatRtf, out string value);
// return the value
return value;
}
与此同时,我找到了另一种解决方法,但您的解决方法要简单得多:-)。我只是删除了最后添加的换行符:
RichEditor.Document.GetText(TextGetOptions.FormatRtf, out var value);
var lastNewLine = value.LastIndexOf("\par", StringComparison.Ordinal);
value = value.Remove(lastNewLine, "\par".Length);
RichEditor.Document.SetText(TextSetOptions.FormatRtf, value);
但这取决于 RichEditBox
的 "wrong" 行为,因此您的解决方案要好得多。
我找到了一个似乎与列表不冲突并且不涉及任何类型的 RTF 文本手动清理的解决方案。
从 ClaudiaWey 的回答中可以看出,在以纯文本格式获取文本时,文本的长度是正确的,至少在使用 LF 时是这样。只有在获取 RTF 格式的文本时才会出现此问题。
所以我的解决方案是将 non-RTF 长度与 RTF 文本内容一起存储,然后在将 RTF 内容加载回文本框时删除该长度与新(错误)长度之间的差异。
在代码形式中,它看起来像这样:
TextBox.TextDocument.GetText(TextGetOptions.FormatRtf, out string savedRichText);
TextBox.TextDocument.GetText(TextGetOptions.UseLf, out string savedText);
var savedLength = savedText.Length;
// persist savedLength to the database or wherever alongside savedRichText...
TextBox.TextDocument.SetText(TextSetOptions.FormatRtf, savedRichText);
// Delete the extra bit that gets added because of a bug
TextBox.TextDocument.GetText(TextGetOptions.UseLf, out string text);
TextBox.TextDocument.GetRange(savedLength, text.Length).Text = "";
我正在尝试在 ;RichEditBox 中获取和设置富文本,但每次执行 GetText 然后再执行 SetText 时,都会添加一个额外的回车符 return。 这是一个超级简单的示例,其中包含一个按钮,该按钮确实会被设置。试试看每次执行 Get-Set 时都会添加一个额外的马车 return。
XAML
<StackPanel>
<Button Content="Get-Set" Click="OnGetSet"/>
<RichEditBox x:Name="RichEditor" Width="300" Height="200"/>
</StackPanel>
C#
private void OnGetSet(object sender, RoutedEventArgs e)
{
RichEditor.Document.GetText(TextGetOptions.FormatRtf, out value);
RichEditor.Document.SetText(TextSetOptions.FormatRtf, value);
}
我在 SetText 和 GetText 中尝试了各种选项,但我能够阻止插入额外的回车 return。 有人有什么建议吗?
我最终找到了一个合理的解决方法。我正在获取完整的文本范围,然后在范围而不是文档上调用 GetText。
我不确定这是否是最佳解决方案,但它工作正常。
已更新 C#
private void OnGetSet(object sender, RoutedEventArgs e)
{
var value = GetText(RichEditor);
RichEditor.Document.SetText(TextSetOptions.FormatRtf, value);
}
public string GetText(RichEditBox editor)
{
// get the actual size of the text
editor.Document.GetText(TextGetOptions.UseLf, out string text);
// get the text in the total range - to avoid getting extra lines
var range = editor.Document.GetRange(0, text.Length);
range.GetText(TextGetOptions.FormatRtf, out string value);
// return the value
return value;
}
与此同时,我找到了另一种解决方法,但您的解决方法要简单得多:-)。我只是删除了最后添加的换行符:
RichEditor.Document.GetText(TextGetOptions.FormatRtf, out var value);
var lastNewLine = value.LastIndexOf("\par", StringComparison.Ordinal);
value = value.Remove(lastNewLine, "\par".Length);
RichEditor.Document.SetText(TextSetOptions.FormatRtf, value);
但这取决于 RichEditBox
的 "wrong" 行为,因此您的解决方案要好得多。
我找到了一个似乎与列表不冲突并且不涉及任何类型的 RTF 文本手动清理的解决方案。
从 ClaudiaWey 的回答中可以看出,在以纯文本格式获取文本时,文本的长度是正确的,至少在使用 LF 时是这样。只有在获取 RTF 格式的文本时才会出现此问题。
所以我的解决方案是将 non-RTF 长度与 RTF 文本内容一起存储,然后在将 RTF 内容加载回文本框时删除该长度与新(错误)长度之间的差异。
在代码形式中,它看起来像这样:
TextBox.TextDocument.GetText(TextGetOptions.FormatRtf, out string savedRichText);
TextBox.TextDocument.GetText(TextGetOptions.UseLf, out string savedText);
var savedLength = savedText.Length;
// persist savedLength to the database or wherever alongside savedRichText...
TextBox.TextDocument.SetText(TextSetOptions.FormatRtf, savedRichText);
// Delete the extra bit that gets added because of a bug
TextBox.TextDocument.GetText(TextGetOptions.UseLf, out string text);
TextBox.TextDocument.GetRange(savedLength, text.Length).Text = "";