繁体   English   中英

如何将.rtf文件导入silverlight 4 richtextbox?

[英]How to import a .rtf file to silverlight 4 richtextbox?

我有一个.rtf文件,并希望将其放入silverlight 4的richtextbox中。不幸的是,我们在silverlight 4 richtextbox中没有.rtf属性,我们只有.xaml。

因此,我要做的是创建一个FlowDocument,然后将.rtf加载到此FlowDocument,然后将其格式化为xaml。 然后将其分配给richtextbox。 但是我有一个例外。

如何将.rtf文件导入silverlight 4 richtextbox?

谢谢!

到目前为止,我使用了一个丑陋的解决方案,使用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);

我建议您改为使用免费的VectorLight Rich Text Box控件。

我需要做类似的事情(还没做...)

我遇到了NRTFTRee ,这是C#RTF解析器,应移植到silverlight。 http://www.codeproject.com/KB/string/nrtftree.aspx

http://nrtftree.sourceforge.net/examples.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM