[英]How to import a .rtf file to silverlight 4 richtextbox?
I have a .rtf file and want to put it in a richtextbox in silverlight 4. Unfortunately we do not have .rtf property in silverlight 4 richtextbox, we only have .xaml. 我有一个.rtf文件,并希望将其放入silverlight 4的richtextbox中。不幸的是,我们在silverlight 4 richtextbox中没有.rtf属性,我们只有.xaml。
So what i did is to create a FlowDocument, than load the .rtf to this FlowDocument, then format it to xaml. 因此,我要做的是创建一个FlowDocument,然后将.rtf加载到此FlowDocument,然后将其格式化为xaml。 then assigned it to richtextbox. 然后将其分配给richtextbox。 But i got a argumentexception. 但是我有一个例外。
How to import a .rtf file to silverlight 4 richtextbox? 如何将.rtf文件导入silverlight 4 richtextbox?
Thanks! 谢谢!
I used an ugly solution so far, use a FlowDocument to change the format from rtf to xaml. 到目前为止,我使用了一个丑陋的解决方案,使用FlowDocument将格式从rtf更改为xaml。 Then remove the attributes not accepted in SL4 richtext box, codes like below. 然后删除SL4 Richtext框中不接受的属性,如下所示。 It works but I hate it. 它有效,但我讨厌它。 I want to know is there a better solution. 我想知道是否有更好的解决方案。
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);
我建议您改为使用免费的VectorLight Rich Text Box控件。
I need to do something similar (haven't done it yet ...) 我需要做类似的事情(还没做...)
I came across NRTFTRee , a C# RTF parser, which should port to silverlight. 我遇到了NRTFTRee ,这是C#RTF解析器,应移植到silverlight。 http://www.codeproject.com/KB/string/nrtftree.aspx http://www.codeproject.com/KB/string/nrtftree.aspx
http://nrtftree.sourceforge.net/examples.html http://nrtftree.sourceforge.net/examples.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.