在 RichTextBox 中从 rtf 复制问题

发布于 2024-09-25 05:30:27 字数 2863 浏览 2 评论 0原文

在序列化丢失的属性时,从 richtextbox 中的 rtf 进行复制时遇到问题

。 // NOT SERIALIZE 属性“bold”、“color”和“size”

所有代码:

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);
}

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

附加文件

I have a problem when copying from rtf in richtextbox

when serializing lost property.
// NOT SERIALIZE properties "bold" and "color" and "size"

All code:

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;
}

main part of the code:

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);
}

if you enter everything manually, all is well. How do I fix this?


attach files

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2024-10-02 05:30:27

嗯,粗体和大小本身并不是属性,它们是 Span 的衍生物,您可能需要通过迭代 Inlines 属性来单独解析它们>Span 来保留它们

Hmm, well bold and size aren't properties as such, they're derivatives of Span, you might need to parse them individually by iterating the Inlines property of your Span to preserve them

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文