C# 第二次XPS导出时发生ObjectDisposed异常

C# 第二次XPS导出时发生ObjectDisposed异常,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个应用程序,用于在文档查看器中打开文件,目的是1:不允许用户更改文件,2:跟踪打开的文件及其打开时间 也就是说,我将他们选择的文件(Word文档或Excel工作簿)转换为XPS文件,并放置在WPF项目的DocumentViewer中 第一次打开文档时,它会按预期工作。但是,一旦尝试打开第二个文件,我就会得到一个 System.ObjectDisposedException:包对象已关闭并已释放,因此无法对此对象或在此包的某个部分上打开的任何流执行操作 我已经找了好几个小时了,不知道发生了什

我有一个应用程序,用于在文档查看器中打开文件,目的是1:不允许用户更改文件,2:跟踪打开的文件及其打开时间

也就是说,我将他们选择的文件(Word文档或Excel工作簿)转换为XPS文件,并放置在WPF项目的DocumentViewer中

第一次打开文档时,它会按预期工作。但是,一旦尝试打开第二个文件,我就会得到一个

System.ObjectDisposedException:包对象已关闭并已释放,因此无法对此对象或在此包的某个部分上打开的任何流执行操作

我已经找了好几个小时了,不知道发生了什么事

以下是相关代码:

class DocumentViewerFileGenerator
     {


        /// <summary>
        /// Generates a docuemnt, of the IDocumentPaginatorSource type to be used by the document viewer in the
        /// view. By looking at the extension type, decides on which interop to use.
        /// </summary>
        /// <param name="filePath">Path of the file that is to be converted</param>
        /// <param name="extension">Extension of the file. Makes it easier for the if's</param>
        /// <returns>A converted IDocumentPaginatorSource version of the file to be viewed.</returns>
        public XpsDocument GenerateDocumentForViewer(string filePath, string extension)
        {

            string tempOutputPath = Environment.CurrentDirectory + @"\temp.xps";
            ClearOldTemp(tempOutputPath);

            XpsDocument xpsDocument;
            if (extension == ".doc" || extension == ".docx")
            {
               ConvertWordToXps(filePath, tempOutputPath);
            }

            if (extension == ".xls" || extension == ".xlsx")
            {
               ConvertExcelToXps(filePath, tempOutputPath);
            }

            xpsDocument = MakeFixedDocument(tempOutputPath);

            return xpsDocument;
        }

        /// <summary>
        /// Just clears out the old temp path
        /// </summary>
        /// <param name="tempOutputPath"></param>
        private void ClearOldTemp(string tempOutputPath)
        {
            if (File.Exists(tempOutputPath))
            {
                File.Delete(tempOutputPath);
            }
        }

        /// <summary>
        /// Converts the file selected, through Word, into an XPS for conversion purposes.
        /// </summary>
        /// <param name="filePath">The file to be converted. Full path needed.</param>
        private void ConvertWordToXps(string filePath, string tempOutputPath)
        {
            Word.Application word = new Word.Application();
            word.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
            word.Visible = false;
            Word.Document document = word.Documents.Open(filePath);
            document.SaveAs2(tempOutputPath, FileFormat: Word.WdSaveFormat.wdFormatXPS);
            document.Close();
            word.Quit();
            Marshal.ReleaseComObject(document);
            Marshal.ReleaseComObject(word);
        }

        /// <summary>
        /// Converts the file selected, through Excel, into an XPS for conversion purposes.
        /// </summary>
        /// <param name="filePath">The file to be converted. Full path needed.</param>
        private void ConvertExcelToXps(string filename, string tempOutputPath)
        {
            Excel.Application excel = new Excel.Application();
            excel.Visible = false;
            excel.DisplayAlerts = false;
            Excel.Workbook workbook = excel.Workbooks.Open(filename);
            workbook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypeXPS, tempOutputPath);
            workbook.Close();
            excel.Quit();
            Marshal.ReleaseComObject(workbook);
            Marshal.ReleaseComObject(excel);
            excel = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tempOutputPath"></param>
        /// <returns></returns>
        private XpsDocument MakeFixedDocument(string tempOutputPath)
        {
            return new XpsDocument(tempOutputPath, FileAccess.Read);
        }


    }
}
抛出异常

我错过了什么


谢谢。

在做了大量的挠头、谷歌搜索和用脸敲击键盘之后,我突然意识到我错过了什么

private void UnlockXPSFile(XpsDocument fileToUnlock)
{
    Package xpsPackage = PackageStore.GetPackage(fileToUnlock.Uri);
    xpsPackage.Close();
    PackageStore.RemovePackage(fileToUnlock.Uri); //This line right here
}

当我关闭包裹时,我实际上并没有将其从包裹商店中移除。因此,应用程序正在寻找同一个包存储区来存储新的XPS文件夹,但由于它被关闭和转储,它没有地方可去。

使用调试器查看调用什么
解锁XPSFile
,它似乎是代码中唯一处理XPS包的地方。@Evk调用它的唯一时间是在
关闭窗口()
方法。选择新文件后,将重新实例化整个viewmodel、view和xps对象本身。这就是为什么我如此困惑。
 private XpsDocument MakeFixedDocument(string tempOutputPath)
 {
      return new XpsDocument(tempOutputPath, FileAccess.Read);
 }
private void UnlockXPSFile(XpsDocument fileToUnlock)
{
    Package xpsPackage = PackageStore.GetPackage(fileToUnlock.Uri);
    xpsPackage.Close();
    PackageStore.RemovePackage(fileToUnlock.Uri); //This line right here
}