I'm currently trying to port Matthew Manela's "Converting between RTF and XAML" code sample to WinRT
I've got the HTML to XAML code working, but I've hit a snag when getting it into a RichEditBox
.
Matthew's code is WPF based, and uses the following function to convert XAML to 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();
}
}
}
I've tried rewriting this in WinRT using RichEditBox
, but come up against some issues. Most noteably, WPF TextRange
accepts a XAML dataformat, but WinRT ITextRange
doesn't have this. However, I know that I can inject XAML directly into a RichTextBlock
control.
Is there any way to copy the text from a RichTextBlock
and paste it into a RichEditBox, programmatically?
OR, failing that, is there a way to convert HTML to RTF that works in WinRT / Windows Store Apps?