将XAML注入RichEditBox

时间:2014-02-24 14:59:23

标签: c# wpf xaml windows-runtime windows-store-apps

我目前正试图将Matthew Manela的"Converting between RTF and XAML"代码示例移植到WinRT

我已经将HTML转换为XAML代码,但是当我将其转换为RichEditBox时,我遇到了麻烦。

Matthew的代码基于WPF,并使用以下函数将XAML转换为RTF。

private static string ConvertXamlToRtf(string xamlText) 
{ 
    var richTextBox = new RichTextBox(); 
    if (string.IsNullOrEmpty(xamlText)) return ""; 
    var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); 
    using (var xamlMemoryStream = new MemoryStream()) 
    { 
        using (var xamlStreamWriter = new StreamWriter(xamlMemoryStream)) 
        { 
            xamlStreamWriter.Write(xamlText); 
            xamlStreamWriter.Flush(); 
            xamlMemoryStream.Seek(0, SeekOrigin.Begin); 
            textRange.Load(xamlMemoryStream, DataFormats.Xaml); 
        } 
    } 
    using (var rtfMemoryStream = new MemoryStream()) 
    { 
        textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); 
        textRange.Save(rtfMemoryStream, DataFormats.Rtf); 
        rtfMemoryStream.Seek(0, SeekOrigin.Begin); 
        using (var rtfStreamReader = new StreamReader(rtfMemoryStream)) 
        { 
            return rtfStreamReader.ReadToEnd(); 
        } 
    } 
}

我尝试使用RichEditBox在WinRT中重写此内容,但遇到了一些问题。最值得注意的是,WPF TextRange接受XAML数据格式,但WinRT ITextRange没有这个格式。但是,我知道我可以将XAML直接注入RichTextBlock控件。

有没有办法从RichTextBlock复制文本并以编程方式将其粘贴到RichEditBox中?

OR ,如果没有,有没有办法将HTML转换为适用于WinRT / Windows应用商店应用的RTF?

1 个答案:

答案 0 :(得分:1)

我在msdn Windows Apps论坛上得到了Rob Caplan的答案

http://social.msdn.microsoft.com/Forums/windowsapps/en-US/c5f4e679-c563-463c-b812-05b16cd5720f/converting-html-to-rtf-using-richeditbox-and-richtextbox?forum=winappswithcsharp

直接引用:

“你必须自己进行转换.RichEdiBox没有任何本机转换能力。它直接渲染RTF而不将其转换为Xaml。这与WPF实现不同,WPF实现转换RTF而不是直接显示它。“

那时候就是这样。我目前正在调整开源RTF库以用于WinRT。完成后我会发布。

编辑

罗布错了! 是可能的,尽管是通过解决方法。我想出了如何使用DataPackage类来完成它。完整答案:https://stackoverflow.com/a/22093837/352867

相关问题