[英]How to close FileStream without causing an error in "Syncfusion.PdfViewer"?
我在我的項目中使用Syncfusion.PdfViewer
。 當我單擊列表中的項目時,相關的 pdf 文件被加載並顯示在PdfViewer
中:
private void PdfReport(string address)
{
//Load the stream from the local system.
FileStream fs = new FileStream(address, FileMode.Open);
PdfSource = fs;
}
問題是每次我加載 pdf 文件時,都會創建一個新的FileStream
實例,並且 memory 的使用量會增加。 當我嘗試像以下代碼一樣關閉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();
}
我怎么解決這個問題?
您應該在創建新文件流之前檢查PdfSource
存在以及是否關閉/處理它,所以只需
if (PdfSource is not null)
PdfSource.Dispose();
PdfSource = new FileStream(address, FileMode.Open);
在 Syncfusion PDFViewer 中,在將 PDF 文檔作為流/文件加載時,它會在內部卸載現有的加載文檔。 在卸載文件時,它會處理文件 stream。 因此,我們不需要在樣本端處理文件 stream。
問候,薩爾曼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.