【问题标题】:How to close FileStream without causing an error in "Syncfusion.PdfViewer"?如何关闭 FileStream 而不会在“Syncfusion.PdfViewer”中导致错误?
【发布时间】:2022-10-02 04:39:52
【问题描述】:

我在我的项目中使用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 的新实例,并且内存使用量会增加。当我尝试像以下代码一样关闭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();
}

我怎么解决这个问题?

    标签: c# wpf filestream syncfusion


    【解决方案1】:

    您应该检查 PdfSource 是否存在,如果如此接近/处置它前创建新的文件流,所以只是

    if (PdfSource is not null)
        PdfSource.Dispose();
    
    PdfSource = new FileStream(address, FileMode.Open);
    

    【讨论】:

      【解决方案2】:

      在 Syncfusion PDFViewer 中,在将 PDF 文档作为流/文件加载时,它会在内部卸载现有的加载文档。在卸载文档时,它会处理文件流。因此,我们不需要在样本端处理文件流。

      【讨论】:

        猜你喜欢
        • 2020-12-28
        • 1970-01-01
        • 2010-12-20
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 2011-10-18
        • 1970-01-01
        相关资源
        最近更新 更多