简体   繁体   English

保存同时包含图像和自定义标记元素的RichTextBox内容

[英]Save RichTextBox content containing both images and custom token elements

Struggling with how to successfully save and load my System.Windows.Controls.RichTextBox content containing all of the following: formatted text, images, custom type-defined token elements, custom dynamic token elements. 努力解决如何成功保存和加载包含以下所有内容的System.Windows.Controls.RichTextBox内容:格式化的文本,图像,自定义类型定义的标记元素,自定义动态标记元素。

By token elements I mean my custom classes inheriting from System.Windows.Documents.Run where type-defined is such that does not need to remember any dynamically set property values (since action is taken based on the type which needs to be remembered after loading) and dynamic ones are such that need to also retain dynamically set properties (action is taken not only based on type, but also based on these set values). 通过标记元素,我的意思是我的自定义类继承自System.Windows.Documents.Run,​​其中类型定义为此类,因此无需记住任何动态设置的属性值(因为基于加载后需要记住的类型采取了操作) )和动态属性,这样还需要保留动态设置的属性(不仅基于类型,而且还基于这些设置值来执行操作)。

I know of the following 3 methods to save/load, neither of which is sufficient: 我知道以下3种保存/加载方法,这两种方法都不足够:

1) 1)

string xamlStream = System.Windows.Markup.XamlWriter.Save(myRichTxtBx.Document);

and then saving the string 然后保存字符串

2) 2)

TextRange content = new TextRange(myRichTxtBx.Document.ContentStart, myRichTxtBx.Document.ContentEnd);
content.Save(myFileStream, DataFormats.XamlPackage, true);

3) 3)

TextRange content = new TextRange(myRichTxtBx.Document.ContentStart, myRichTxtBx.Document.ContentEnd);
content.Save(myFileStream, DataFormats.Xaml, true);

These are the problems with those: 这些是这些问题:

1) unable to load image after restarting the application (but remembers properties) 1)重新启动应用程序后无法加载图像(但记住属性)

2) does not remember the properties (but is able to load image after restarting the app) 2)不记得属性(但是能够在重新启动应用程序后加载图像)

3) won't load image not even in the same instance of the app and also does not remember the property values 3)即使在同一应用程序实例中也不会加载图像,并且也不记得属性值

I could only find answers resolving image saving issues (2) or property issues (1), but never both. 我只能找到解决图像保存问题(2)或属性问题(1)的答案,但不能同时解决这两个问题。

The goal is to have a tokenizable RichTextBox, where tokens are either replaced by values from database based on provided ORM object (= type-defined token) or by dynamically set values by user based again on a provided ORM object. 目标是拥有可标记化的RichTextBox,其中标记可被基于提供的ORM对象(=类型定义的标记)的数据库值替换,或由用户再次基于提供的ORM对象动态设置值。

I have solved the issue by a very ugly workaround: 我已经通过一个非常丑陋的解决方法解决了这个问题:

To save the document I use the method (1) described above. 为了保存文档,我使用上述方法(1)。 Before this, I traverse the FlowDocument by a custom walker and replace each image element with a custom inline token element (much like the other tokens). 在此之前,我通过自定义遍历器遍历FlowDocument,并用自定义内嵌标记元素替换每个图像元素(与其他标记非常相似)。 A hash ID is assigned as a property to this substitute element and the image itself is saved having the hash as its file name (serves to identify token with the image file). 哈希ID被分配为该替换元素的属性,并且以哈希作为文件名保存图像本身(用于与图像文件一起标识令牌)。 Images, along with the main document (saved by method (1)), are packaged into a single file. 图像与主文档(通过方法(1)保存)一起被打包到一个文件中。

When loading everything back, package is unpacked, document loaded keeping the tokens with their properties and substitute image elements are replaced by the actual images from the files saved in the package using once again the before-mentioned custom walker and the established hash token-file relation. 重新加载所有内容时,将解包软件包,并使用先前的自定义walker和已建立的哈希令牌文件,再次使用保留在软件包中的文件中的实际图像替换加载的文件,以保留其令牌和其属性,并替换图像元素。关系。

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

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