我到目前为止使用了一个丑陋的解决方案,使用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);
嗯......眼看Silverlight不对这一切支持FlowDocument的是一个有点混乱。 – AnthonyWJones 2010-07-23 15:14:36
是的,你是对的。 Silverlight不支持Flowdocument,我所做的是在webservice中使用FlowDocument,然后Silverlight可以与webservice交谈 – fresky 2010-07-28 07:28:55