RichTextBox 中的段落标签可以保存或加载吗?

Can Paragraph Tags in a RichTextBox be Saved or Loaded?

RichTextBox 中,我使用段落 Tag 来获取信息,例如段落主题,例如

string[] np = new string[]
{
    "B-CC:", "B-HPI:", "B-ROS:", "B-PMH:", "B-PSH:"
};

RichTextControl.Document.Blocks.Clear();
foreach (var s in np)
{
    var p = new Paragraph();
    p.Tag = s;
    RichTextControl.Document.Blocks.Add(p);
}

标签中的信息能否保存并在以后从 MemoryStream 中调用?

TIA

您可以使用 XamlWrite.Save 方法序列化 RichTextBox,包括 ParagraphTag 属性 值:

//serialize:
string xaml = System.Windows.Markup.XamlWriter.Save(RichTextControl);
//de-serialize:
RichTextBox rtb = System.Windows.Markup.XamlReader.Parse(xaml) as RichTextBox;
foreach(var p in rtb.Document.Blocks.OfType<Paragraph>())
{
    if(p.Tag != null)
    {
        string tag = p.Tag.ToString();
    }
}