来自richtextbox的rtf的copyn'g问题

时间:2010-09-25 12:39:01

标签: c# wpf richtextbox

从richtextbox

中的rtf复制时出现问题

序列化丢失的财产时。 // NOT SERIALIZE属性“粗体”和“颜色”和“大小”

所有代码:

string ConvertXamlToString(FlowDocument fd)
{
 string format = "@TAG@{0}:{1}@TAG@";

 FlowDocument ss = new FlowDocument();

 for (int i = 0; i < fd.Blocks.Count; i++)
 {
  var block = (fd.Blocks as BlockCollection).ElementAt(i);
  if (block is Paragraph)
  {
   var p = new Paragraph();
   for (int y = 0; y < ((Paragraph)block).Inlines.Count; y++)
   {
    var inline = ((Paragraph)block).Inlines.ElementAt(y);
    if (inline is InlineUIContainer)
    {
     var elem = ((InlineUIContainer)inline).Child;
     if (elem is FlashControl)
     {
      TextBox mc = new TextBox() { Text = string.Format(format, "FlashControl", (elem as FlashControl).Flashp.Source) };
      p.Inlines.Add(mc);
     }
     else if (elem is MusicControl)
     {
      MusicControl mc = new MusicControl((elem as MusicControl).Path_file);
      p.Inlines.Add(mc);
     }
     else if (elem is Image)
     {
      Image mc = new Image();
      Image Last = (elem as Image);
      try
      {
       if (Last.Source is System.Windows.Media.Imaging.BitmapImage)
       {
        mc.Source = new BitmapImage(new Uri(((System.Windows.Media.Imaging.BitmapImage)(Last.Source)).UriSource.AbsolutePath));
       }
       else if(Last.Source is System.Windows.Media.Imaging.BitmapImage)
       {
        mc.Source = new BitmapImage(new Uri(((System.Windows.Media.Imaging.BitmapImage)(Last.Source)).UriSource.ToString()));
       }
      }
      catch { }
      p.Inlines.Add(mc);
     }
     else
     {
      p.Inlines.Add(elem);
     }
    }
    else if (inline is Run)
    {
     Run r = (inline as Run);
     string rSer = XamlWriter.Save(r);
     var inl1 = XamlReader.Parse(rSer);
     p.Inlines.Add(inl1 as Run);
    }
    else if (inline is Span)
    {
     Span r = (inline as Span);
     string rSer = XamlWriter.Save(r);// NOT SERIALIZE properties "bold" and "color" and "size"
     var inl1 = XamlReader.Parse(rSer);
     p.Inlines.Add(inl1 as Span);
    }
    else
    {
    }
   }
   ss.Blocks.Add(p);
  }
 }

 string aaa = XamlWriter.Save(ss);

 richtextbox.Document.Blocks.Clear();

 object f = XamlReader.Parse(aaa);
 richtextbox.Document = f as FlowDocument;
 return aaa;
}

代码的主要部分:

else if (inline is Run)
{
    Run r = (inline as Run);
    string rSer = XamlWriter.Save(r);
    var inl1 = XamlReader.Parse(rSer);
    p.Inlines.Add(inl1 as Run);
}
else if (inline is Span)
{
    Span r = (inline as Span);
    string rSer = XamlWriter.Save(r);// NOT SERIALIZE properties "bold" and "color" and "size"
    var inl1 = XamlReader.Parse(rSer);
    p.Inlines.Add(inl1 as Span);
}

如果您手动输入所有内容,一切都很顺利。我该如何解决这个问题?

attach files

1 个答案:

答案 0 :(得分:0)

嗯,粗体和大小不是属性,它们是Span的衍生物,您可能需要通过迭代Inlines的{​​{1}}属性来单独解析它们保护他们