.NET WPF 应用程序:加载资源 .XPS 文档

发布于 2024-09-04 00:29:01 字数 897 浏览 13 评论 0原文

我正在尝试将 .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 技术交流群。

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

发布评论

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

评论(2

香橙ぽ 2024-09-11 00:29:01

XpsDocument ctor 接受文件路径或 Package 实例。以下是打开包以使用后一种方法的方法:

var uri = new Uri("pack://application:,,,/Docs/Mydocument.xps");
var stream = Application.GetResourceStream(uri).Stream;
Package package = Package.Open(stream);
PackageStore.AddPackage(uri, package);
var xpsDoc = new XpsDocument(package, CompressionOption.Maximum, uri.AbsoluteUri);
var fixedDocumentSequence = xpsDoc.GetFixedDocumentSequence();
_vw.Document = fixedDocumentSequence; // displaying document in viewer
xpsDoc.Close();

XpsDocument ctor accepts either a file path or a Package instance. Here's how you can open a Package to use the latter approach:

var uri = new Uri("pack://application:,,,/Docs/Mydocument.xps");
var stream = Application.GetResourceStream(uri).Stream;
Package package = Package.Open(stream);
PackageStore.AddPackage(uri, package);
var xpsDoc = new XpsDocument(package, CompressionOption.Maximum, uri.AbsoluteUri);
var fixedDocumentSequence = xpsDoc.GetFixedDocumentSequence();
_vw.Document = fixedDocumentSequence; // displaying document in viewer
xpsDoc.Close();
雪化雨蝶 2024-09-11 00:29:01

我无法使用包加载 XPS 文档,并且在任何情况下,将文档包装在包中以便能够加载它似乎都是不必要的解决方法。

如果将 XPS 文档的构建操作设置为 Resource 不是硬性要求,则可以通过将文档的构建操作设置为 Content 来实现更简单的解决方案(并设置“复制到输出目录”)。

var docPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Docs/MyDocument.xps");
using var document = new XpsDocument(termsPath, FileAccess.Read);
_vw.Document = document.GetFixedDocumentSequence();
document.Close()

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 to Content (and setting "Copy to Output Directory").

var docPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Docs/MyDocument.xps");
using var document = new XpsDocument(termsPath, FileAccess.Read);
_vw.Document = document.GetFixedDocumentSequence();
document.Close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文