.net - WPF DocumentViewer不会发布XPS文件

标签 .net wpf xps documentviewer

我正在打开和显示XPS文档的WPF应用程序上工作。当应用程序关闭时,规范是应用程序应删除打开的XPS文档进行清理。但是,当打开某个XPS文档时,该应用程序将引发一个异常,即在尝试删除该文件时该文件仍在使用中。这有点奇怪,因为它仅在打开特定的XPS文档时发生,并且仅在您移出首页之后才发生。

我使用的一些代码如下所示:

要打开XPS文档:

DocumentViewer m_documentViewer = new DocumentViewer();
XpsDocument m_xpsDocument = new XpsDocument(xpsfilename, fileaccess);
m_documentViewer.Document = m_xpsDocument.GetFixedDocumentSequence();
m_xpsDocument.Close();

要浏览XPS文档:
m_documentViewer.FirstPage();
m_documentViewer.LastPage();
m_documentViewer.PreviousPage();
m_documentViewer.NextPage();

要关闭DocumentViewer对象并删除文件,请执行以下操作:
m_documentViewer.Document = null;
m_documentViewer = null;
File.Delete(xpsfilename);

这都是非常基本的,并且可以与我们测试的其他文档一起使用。但是对于特定的XPS文档,会弹出一个异常,指出要删除的文件仍在使用中。

我的代码有什么错误或遗漏吗?

谢谢!

最佳答案

使xpsDocument成为成员,然后不要在其上调用close():)

关于.net - WPF DocumentViewer不会发布XPS文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/283027/

相关文章:

c# - 动态返回模型的值

c# - Convert.ToInt32(String) on String.Empty 与 Null

c# - 使用 C# 将 PDF 转换为 XPS 的免费方法

pdf - Ghostscript:使用 'gxps' 生成可搜索的 PDF

c# - 如何动态创建xps文件?

c# - 如何将 Lucene.NET RangeQuery 转换为 NumericRangeQuery 以进行日期范围搜索?

.net - 如何在 MEF 中将类型从 typename 传递到 GetExports?

wpf - 与 TextBlock 具有相同剪辑的 TextBox(TextBox 模板)

wpf - 每当 ListView 子控件获得焦点时,如何更新 ListViewItem.SelectedItem?

c# - 后面的wpf绑定(bind)代码