我有一个.rtf文件,想把它放在silverlight 4的richtextbox中。不幸的是我们在silverlight 4 richtextbox中没有.rtf属性,我们只有.xaml。
所以我做的是创建一个FlowDocument,而不是将.rtf加载到此FlowDocument,然后将其格式化为xaml。然后将其分配给richtextbox。但我得到了一个争论的例子。
如何将.rtf文件导入silverlight 4 richtextbox?
谢谢!
答案 0 :(得分:0)
到目前为止,我使用了一个丑陋的解决方案,使用FlowDocument将格式从rtf更改为xaml。然后删除SL4 richtext框中未接受的属性,代码如下所示。它有效,但我讨厌它。 我想知道是否有更好的解决方案。
string xaml = String.Empty;
FlowDocument doc = new FlowDocument();
TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
using(StreamWriter sw = new StreamWriter(ms))
{
sw.Write(from);
sw.Flush();
ms.Seek(0, SeekOrigin.Begin);
range.Load(ms, DataFormats.Rtf);
}
}
using(MemoryStream ms = new MemoryStream())
{
range = new TextRange(doc.ContentStart, doc.ContentEnd);
range.Save(ms, DataFormats.Xaml);
ms.Seek(0, SeekOrigin.Begin);
using (StreamReader sr = new StreamReader(ms))
{
xaml = sr.ReadToEnd();
}
}
// remove all attribuites in section and remove attribute margin
int start = xaml.IndexOf("<Section");
int stop = xaml.IndexOf(">") + 1;
string section = xaml.Substring(start, stop);
xaml = xaml.Replace(section, "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
xaml = xaml.Replace("Margin=\"0,0,0,0\"", String.Empty);
答案 1 :(得分:0)
答案 2 :(得分:0)
我需要做类似的事情(还没有做过......)
我遇到了NRTFTRee,一个C#RTF解析器,应该移植到silverlight。 http://www.codeproject.com/KB/string/nrtftree.aspx