我到目前爲止使用了一個醜陋的解決方案,使用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