Sau khi hiển thị tệp XPS trong DocumentViewer WPF và đóng cá thể DocumentViewer, tệp XPS bị khóa và tôi không thể xóa nó. Tôi cần phải giải phóng khóa trên tệp XPS để tôi có thể xóa nó, viết một tệp khác có cùng tên và hiển thị tùy chọn tệp XPS mới đó trong một cá thể DocumentViewer mới. Tôi cần phải làm điều này trong cùng một trường hợp ứng dụng - mà không cần phải đóng ứng dụng (đây là một kịch bản Xem trước bản in).Làm cách nào để lấy DocumentViewer của WPF để giải phóng khóa tệp của nó trên Tài liệu XPS nguồn?
Nói cách khác, làm cách nào tôi nhận được mã sau để chạy mà không ném ngoại lệ vào "File.Delete (tempXpsFile);" tuyên bố?
var tempXpsFile = @"c:\path\to\Temporary.xps";
var previewWindow = new Window();
var docViewer = new DocumentViewer();
previewWindow.Content = docViewer;
GenerateXpsFile(tempXpsFile);
var xpsDocument = new XpsDocument(tempXpsFile);
previewWindow.ShowDialog();
File.Delete(tempXpsFile); //this will throw an exception due to a file lock on tempXpsFile
GenerateXpsFile(tempXpsFile); //assume this generates a different file
//otherwise the scenario doesn't make sense as we could just skip the above delete
//and this statement and re-use the same file
previewWindow = new Window();
docViewer = new DocumentViewer();
previewWindow.Content = docViewer;
previewWindow.ShowDialog();
Đóng ứng dụng không giải phóng khóa tệp, như đã đề cập trong WPF DocumentViewer doesn't release the XPS file, nhưng đó không phải là tùy chọn trong trường hợp này.
Điều này bắt gặp tôi, tôi có một người xem tải xuống một tài liệu, một khi nó đã được thiết lập tài liệu nó sẽ thất bại ngay cả sau khi áp dụng những thay đổi này.Khi tôi đang tải tài liệu, tôi đã xử lý tài liệu gốc sẽ khiến bản phát hành thất bại khi tôi tải lại tài liệu. –