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

标签 c# uwp uwp-xaml

在尝试将 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/

相关文章:

xaml - UWP ListView DataTemplate 绑定(bind)到项目而不是属性

uwp - 将NavigationView添加到XAML : "Cannot find a Resource with the Name/Key NavigationViewItemDefaultStyle"

c# - 如何更改 asp.net 复选框/单选按钮 css 并正确运行 atserver

c# - 使用自定义表达式扩展 EF Core 'where' 子句

c# - if-else 组合框

visual-studio - 最低 SDK 版本 Windows 10 (UWP)

c# - 如何在 UWP 中输入 PointerEnter 时更改 HyperlinkBut​​ton 背景和前景色

xaml - 如何在App.xaml中设置全局自定义字体

c# - 如何私下使用接口(interface)

c# - 如何从日期时间对象中删除年份?