简体   繁体   English

如何关闭 FileStream 而不会导致“Syncfusion.PdfViewer”出现错误?

[英]How to close FileStream without causing an error in "Syncfusion.PdfViewer"?

I'm using Syncfusion.PdfViewer in my project.我在我的项目中使用Syncfusion.PdfViewer When I click an item in a list, the related pdf file is loaded and shown in the PdfViewer :当我单击列表中的项目时,相关的 pdf 文件被加载并显示在PdfViewer中:

private void PdfReport(string address)
{
    //Load the stream from the local system.
    FileStream fs = new FileStream(address, FileMode.Open);
    PdfSource = fs;
}

The problem is that each time I load a pdf file, a new instance of FileStream is created and the memory usage increases.问题是每次我加载 pdf 文件时,都会创建一个新的FileStream实例,并且 memory 的使用量会增加。 When I try to close FileStream like the following code, the pdf is not shown in the viewer:当我尝试像以下代码一样关闭FileStream时,查看器中未显示 pdf:

private void PdfReport(string address)
{
    //Load the stream from the local system.
    FileStream fs = new FileStream(address, FileMode.Open);
    PdfSource = fs;
    fs.Dispose();
}

How can I solve this problem?我怎么解决这个问题?

You should check if a PdfSource exists and if so close/ dispose that before creating the new filestream, so just您应该在创建新文件流之前检查PdfSource存在以及是否关闭/处理它,所以只需

if (PdfSource is not null)
    PdfSource.Dispose();

PdfSource = new FileStream(address, FileMode.Open);

In Syncfusion PDFViewer, while loading the PDF document as stream/file, it unloads the existing loaded document internally.在 Syncfusion PDFViewer 中,在将 PDF 文档作为流/文件加载时,它会在内部卸载现有的加载文档。 While unloading the document, it will dispose the file stream.在卸载文件时,它会处理文件 stream。 Hence, we don't need to dispose the file stream on the sample side.因此,我们不需要在样本端处理文件 stream。

Regards, Salman问候,萨尔曼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM