2010-07-23 62 views
2

我有一個.rtf文件,並希望將其放在silverlight 4的richtextbox中。不幸的是,我們沒有.light文件夾中的.rtf屬性,我們只有.xaml。如何將.rtf文件導入到silverlight 4 richtextbox?

所以我所做的就是創建一個FlowDocument,然後將這個.rtf加載到這個FlowDocument中,然後將其格式化爲xaml。然後將其分配給richtextbox。但我得到了一個理論上的假象。

如何將.rtf文件導入到silverlight 4 richtextbox?

謝謝!

回答

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); 
+0

嗯......眼看Silverlight不對這一切支持FlowDocument的是一個有點混亂。 – AnthonyWJones 2010-07-23 15:14:36

+0

是的,你是對的。 Silverlight不支持Flowdocument,我所做的是在webservice中使用FlowDocument,然後Silverlight可以與webservice交談 – fresky 2010-07-28 07:28:55

0

我建議你看看免費的VectorLight Rich Text Box控制。

+0

我檢查過vectorlight richtextbox的api,似乎不直接支持rtf。 – fresky 2010-07-24 06:37:08

+0

哦,是的,Vectorlight只使用XML richtext格式,而不是RTF格式。類似的名字,但不同的格式。 – 2011-01-16 04:39:18

相關問題