文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>silverlight中的richtextbox和rtf格式的转换

silverlight中的richtextbox和rtf格式的转换

时间:2010-10-09  来源:fresky

silvergliht 4.0中有了richtextbox控件,但是它只接受xaml格式的输入,下面这个函数实现了rtf和xaml的转换,用到了flowdocument,只是实现了功能,代码有待优化,呵呵。

需要注意的是直接从rtf转换过来的话有些attribute silverlight不认。我也不知道找全了没有,目前发现的是 section 里面的一堆attribute,然后就是margin和textindent。另外中文字体也会出错。

 

    1         /// <summary>

    2         /// convert the string "from" from sourceFormat to targetFormat

    3         /// </summary>

    4         /// <param name="from"></param>

    5         /// <param name="sourceFormat">System.Windows.DataFormats.Rtf</param>

    6         /// <param name="targetFormat">System.Windows.DataFormats.Xaml</param>

    7         /// <returns></returns>

    8         private string convertFormat(string from, string sourceFormat, string targetFormat)

    9         {

   10             string xaml = String.Empty;

   11             System.Windows.Documents.FlowDocument doc = new System.Windows.Documents.FlowDocument();

   12             System.Windows.Documents.TextRange range = new System.Windows.Documents.TextRange(doc.ContentStart, doc.ContentEnd);

   13 

   14             using (MemoryStream ms = new MemoryStream())

   15             {

   16                 using (StreamWriter sw = new StreamWriter(ms))

   17                 {

   18                     sw.Write(from);

   19                     sw.Flush();

   20                     ms.Seek(0, SeekOrigin.Begin);

   21                     range.Load(ms, sourceFormat);

   22                 }

   23             }

   24 

   25 

   26             using (MemoryStream ms = new MemoryStream())

   27             {

   28                 range = new System.Windows.Documents.TextRange(doc.ContentStart, doc.ContentEnd);

   29 

   30                 range.Save(ms, targetFormat);

   31                 ms.Seek(0, SeekOrigin.Begin);

   32                 using (StreamReader sr = new StreamReader(ms))

   33                 {

   34                     xaml = sr.ReadToEnd();

   35                 }

   36             }

   37 

   38             // remove all attribuites in section and remove attribute margin

   39             if (targetFormat.Equals(System.Windows.DataFormats.Xaml))

   40             {

   41                 int start = xaml.IndexOf("<Section");

   42                 int stop = xaml.IndexOf(">") + 1;

   43 

   44                 string section = xaml.Substring(start, stop);

   45 

   46                 xaml = xaml.Replace(section, "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");

   47 

   48                 // remove Margin attribute

   49                 Regex reg = new Regex("Margin=\"[^\"]*\"");

   50                 MatchCollection matches = reg.Matches(xaml);

   51                 if (matches != null && matches.Count > 0)

   52                 {

   53                     int count = matches.Count;

   54                     for (int i = 0; i < count; i++)

   55                     {

   56                         xaml = xaml.Replace(matches[i].ToString(), string.Empty);

   57                     }

   58                 }

   59 

   60                 // remove TextIndent attribute

   61                 reg = new Regex("TextIndent=\"[^\"]*\"");

   62                 matches = reg.Matches(xaml);

   63                 if (matches != null && matches.Count > 0)

   64                 {

   65                     int count = matches.Count;

   66                     for (int i = 0; i < count; i++)

   67                     {

   68                         xaml = xaml.Replace(matches[i].ToString(), string.Empty);

   69                     }

   70                 }

   71 

   72                 // change the non english font family

   73                 reg = new Regex("FontFamily=\"[^a-zA-Z]*\"");

   74                 matches = reg.Matches(xaml);

   75                 if (matches != null && matches.Count > 0)

   76                 {

   77                     int count = matches.Count;

   78                     for (int i = 0; i < count; i++)

   79                     {

   80                         xaml = xaml.Replace(matches[i].ToString(), "FontFamily=\"Arial\"");

   81                     }

   82                 }

   83 

   84             }

   85             return xaml;

   86         }



相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载