gpt4 book ai didi

uwp - 复制 UWP RichEditBox 内容会添加额外的行

转载 作者:行者123 更新时间:2023-12-02 03:27:54 25 4
gpt4 key购买 nike

我正在尝试在 ;RichEditBox 中获取和设置富文本,但每次执行 GetText 然后执行 SetText 时,都会添加一个额外的回车符。这是一个 super 简单的示例,其中包含一个按钮,该按钮确实会被设置。试试看,每次执行 Get-Set 时都会添加一个额外的回车符。

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 中尝试了各种选项,但我能够阻止插入额外的回车符。有人有什么建议吗?

最佳答案

我最终找到了一个合理的解决方法。我正在获取完整的文本范围,然后在该范围而不是文档上调用 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;
}

关于uwp - 复制 UWP RichEditBox 内容会添加额外的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com