gpt4 book ai didi

c# - UWP 与 ITextRange 接口(interface)的使用。 WPF 中的 TextRange 对象

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:41 26 4
gpt4 key购买 nike

在尝试将 WPF 代码(显示在最后)转换为 UWP 代码时,我遇到了两个挑战:

  1. UWP 相当于 TexRange WPF 中的对象。
  2. UWP 相当于 TexRange.Load(...) WPF 中的方法。

我创建了一个 TextRange在 UWP 中如下所示:

var richTextBox = new RichEditBox();
richTextBox.Document.GetText(TextGetOptions.None, out string rebText);
ITextRange textRange = richTextBox.Document.GetRange(0, rebText.Length-1);

但是UWP的ITextRange对象好像没有Load()方法。

问题:

  1. UWP 中的上述textRange 是否与以下WPF 代码中WPF 的textRange 对象执行相同;或者它们在 UWP 中的行为与在 WPF 中的行为不同?
  2. 由于 UWP 的 ITextRange 对象没有 .Load(..) 方法,我该如何处理 UWP 代码中的 Load(…) 方法,同时将以下 WPF 代码转换为 UWP 应用?

要迁移到 UWP 应用程序的 WPF 代码:

private static string ConvertRtfToXaml(string rtfText)
{
var richTextBox = new RichTextBox();
if (string.IsNullOrEmpty(rtfText)) return "";

var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

//Create a MemoryStream of the Rtf content

using (var rtfMemoryStream = new MemoryStream())
{
using (var rtfStreamWriter = new StreamWriter(rtfMemoryStream))
{
rtfStreamWriter.Write(rtfText);
rtfStreamWriter.Flush();
rtfMemoryStream.Seek(0, SeekOrigin.Begin);

//Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
textRange.Load(rtfMemoryStream, DataFormats.Rtf);
}
}

using (var rtfMemoryStream = new MemoryStream())
{

textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
textRange.Save(rtfMemoryStream, DataFormats.Xaml);
rtfMemoryStream.Seek(0, SeekOrigin.Begin);
using (var rtfStreamReader = new StreamReader(rtfMemoryStream))
{
return rtfStreamReader.ReadToEnd();
}
}
}

最佳答案

Since ITextRange object of UWP does not have a .Load(..) method, how do I deal with the Load(…) method in my UWP code while converting the following code of WPF into UWP app?

在 UWP 中,您可以使用 ITextDocument.SaveToStreamITextDocument.LoadFromStream方法。

您可以在 RichEditBox 上查看样本文档。

关于c# - UWP 与 ITextRange 接口(interface)的使用。 WPF 中的 TextRange 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55659983/

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