将 WPF FlowDocuments 序列化到 PackagePart 或从 PackagePart 序列化(包括图像等)
我希望能够在单个包中存储多个 FlowDocuments,包括每个文档中的图像等。但是,我见过的用于保存(和加载)Xaml FlowDocuments 的方法似乎都无法做到这一点。
- TextRange.Save with DataFormats.Xaml 剥离图像和其他嵌入内容
- TextRange.Save with DataFormats.XamlPackage 创建一个全新的包,而不是允许我将文档和包含的图像视为包中的一部分,我将其存储在
- XamlWriter 中看起来这对此很有好处,但我不知道如何找到所有嵌入的对象以放入它们自己的部分(尽管我当然知道如何在找到它们后处理它们)。另一方面,我不知道如何让所有内容稍后正确加载。
非常烦人的是,没有一站式方法将 FlowDocument 及其图像等序列化为 PackagePart。如果有人找到了这样做的好方法,你是如何实现的?
更新2011-07-03 00:22:使用XamlWriter和来自这个问题 我已经能够构建一个快乐的小型 OPC 兼容包,它可以容纳多个 FlowDocument,包括他们的图像,作为 PackageParts。然而,走另一种方式(从 PackagePart 到 FlowDocument)是失败的,因为无论我如何尝试加载文档,我都会收到 XamlParseExceptions 告诉我
“‘System.Windows.Media.Imaging.BitmapImage’初始化引发异常。”
因此,现在的问题是,如何处理 XamlReader.Load 和/或我的部分流以便正确加载相关图像?
I want to be able to store multiple FlowDocuments within a single package, including images, etc. within each document. However, none of the methods I've seen for saving (and loading) Xaml FlowDocuments seem capable of this.
- TextRange.Save with DataFormats.Xaml strips images and other embedded content
- TextRange.Save with DataFormats.XamlPackage creates a whole new package, rather than allowing me to treat the document and included images as parts within the package I'd be storing it in
- XamlWriter looks like it could be good for this, but I can't figure out how to find all the embedded objects for putting in their own parts (although I certainly know how to handle them once I've found them). On the other end, I haven't a clue how to make everything load properly later on.
It's pretty annoying that there's no one-stop way of serializing a FlowDocument and its images, etc. to a PackagePart. If anyone's figured out a good way of doing this, how'd you pull it off?
UPDATE 2011-07-03 00:22: Using XamlWriter and some extra code from this question I've been able to build a happy little OPC-compliant package which can hold multiple FlowDocuments including their images, as PackageParts. However, going the other way (from PackagePart to FlowDocument) is failing, because no matter how I try to load the document, I get XamlParseExceptions telling me that
'Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception.'
So, the question now becomes, how do I manhandle XamlReader.Load and/or my part's stream in order to get the related images loaded properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想通了。解决方案是在将 Xaml 文档交给 XamlReader 之前手动对其进行处理。图像(以及存储为自己的 PackagePart 的其他元素)需要将 BitmapImage.UriSource 属性设置为包含包 Uri(例如,将 /Content/Document.xaml 中的“./Image1.png”设置为“pack://file” :,,,C:,项目,Package.pak/Content/Image1.png")。
不过,有两个警告:
但是
PackUriHelper.Create(Uri,Uri) 存在问题
。而不是使用你必须使用
其中
value
是part.Uri,删除了首字母/。如果您不这样做,您将得到一个在包文件名后面带有额外逗号的 Uri,而不是像上面那样获得正确的 Uri,这会让 XamlReader 感到困惑和烦恼。打开包时需要使用 FileShare.Read,因为 XamlReader 会尝试自行打开它。默认情况下,Package.Open 会锁定任何其他尝试打开包的人,而 XamlReader.Load 如果无法进入包本身,则会抛出 WebException。
Figured it out. The solution is to manually process the Xaml document before handing it over to XamlReader. Images (and other elements stored as their own PackageParts) need to have the BitmapImage.UriSource property set to include the package Uri (for example, "./Image1.png" in /Content/Document.xaml to "pack://file:,,,C:,Projects,Package.pak/Content/Image1.png").
Two caveats, however:
There's an issue with
PackUriHelper.Create(Uri,Uri)
however. Instead of usingyou have to use
where
value
is part.Uri with the initial / removed. If you don't do this, instead of getting a proper Uri like above, you get one with an additional comma after the package file name, which confuses and annoys XamlReader.You need to use FileShare.Read when opening the package, as XamlReader will try and open it itself. By default, Package.Open locks out anyone else trying to open the package, and XamlReader.Load will throw a WebException if it can't get into the package itself.