.NET WPF 应用程序:加载资源 .XPS 文档
我正在尝试将 .xps 文档加载到 WPF 应用程序中的 DocumentViewer 对象中。一切正常,除了当我尝试加载资源 .xps 文档时。使用绝对路径时,我能够很好地加载 .xps 文档,但是当我尝试加载资源文档时,它会抛出“DirectoryNotFoundException”
这是我加载文档的代码示例。
using System.Windows.Xps.Packaging;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Absolute Path works (below)
//var xpsDocument = new XpsDocument(@"C:\Users\..\Visual Studio 2008\Projects\MyProject\MyProject\Docs\MyDocument.xps", FileAccess.Read);
//Resource Path doesn't work (below)
var xpsDocument = new XpsDocument(@"\MyProject;component/Docs/Mydocument.xps", FileAccess.Read);
DocumentViewer.Document = xpsDocument.GetFixedDocumentSequence();
}
当抛出 DirectoryNotFoundException 时,它显示“无法找到路径的一部分:'C:\MyProject;component\Docs\MyDocument.xps'
似乎它正在尝试从该路径获取 .xps 文档,就好像它是计算机上的实际路径,而不是试图从作为资源存储在应用程序中的 .xps 中获取。
I'm trying to load a .xps document into a DocumentViewer object in my WPF application. Everything works fine, except when I try loading a resourced .xps document. I am able to load the .xps document fine when using an absolute path, but when I try loading a resourced document it throws a "DirectoryNotFoundException"
Here's an example of my code that loads the document.
using System.Windows.Xps.Packaging;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Absolute Path works (below)
//var xpsDocument = new XpsDocument(@"C:\Users\..\Visual Studio 2008\Projects\MyProject\MyProject\Docs\MyDocument.xps", FileAccess.Read);
//Resource Path doesn't work (below)
var xpsDocument = new XpsDocument(@"\MyProject;component/Docs/Mydocument.xps", FileAccess.Read);
DocumentViewer.Document = xpsDocument.GetFixedDocumentSequence();
}
When the DirectoryNotFoundException is thrown, it says "Could not find a part of the path : 'C:\MyProject;component\Docs\MyDocument.xps'
It appears that it is trying to grab the .xps document from that path, as if it were an actual path on the computer, and not trying to grab from the .xps that is stored as a resource within the application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XpsDocument
ctor
接受文件路径或Package
实例。以下是打开包以使用后一种方法的方法:XpsDocument
ctor
accepts either a file path or aPackage
instance. Here's how you can open a Package to use the latter approach:我无法使用包加载 XPS 文档,并且在任何情况下,将文档包装在包中以便能够加载它似乎都是不必要的解决方法。
如果将 XPS 文档的构建操作设置为
Resource
不是硬性要求,则可以通过将文档的构建操作设置为Content
来实现更简单的解决方案(并设置“复制到输出目录”)。I couldn't get the XPS document to load using a package, and in any case it seems like an unnecessary workaround to wrap the document in a package to be able to load it.
If it is not a hard requirement to set the build action of the XPS document to
Resource
, then a much simpler solution is possible by setting the build action of the document toContent
(and setting "Copy to Output Directory").