Fix PDF thumbnail rendering crash #1531

This commit is contained in:
ema
2025-07-01 04:30:08 +08:00
parent 72c8e567cd
commit 1f13aa6678
2 changed files with 10 additions and 2 deletions

View File

@@ -18,6 +18,7 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
@@ -30,7 +31,7 @@ internal class AsyncPageToThumbnailConverter : IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{ {
if (values.Length < 2) if (values.Length < 3)
throw new Exception("PageIdToImageConverter"); throw new Exception("PageIdToImageConverter");
if (values[0] is not PdfDocumentWrapper handle) return null; if (values[0] is not PdfDocumentWrapper handle) return null;
@@ -38,11 +39,17 @@ internal class AsyncPageToThumbnailConverter : IMultiValueConverter
var pageId = (int)values[1]; var pageId = (int)values[1];
if (pageId < 0) return null; if (pageId < 0) return null;
if (values[2] is not UserControl thisPdfViewer) return null;
var task = Task.Run(() => var task = Task.Run(() =>
{ {
try try
{ {
return handle.RenderThumbnail(pageId); if (handle is null || thisPdfViewer is null) return Loading;
return thisPdfViewer.Dispatcher.Invoke(() =>
{
return handle.RenderThumbnail(pageId);
});
} }
catch (Exception) catch (Exception)
{ {

View File

@@ -62,6 +62,7 @@
<MultiBinding Converter="{StaticResource AsyncPageToThumbnailConverter}"> <MultiBinding Converter="{StaticResource AsyncPageToThumbnailConverter}">
<Binding ElementName="thisPdfViewer" Path="PdfDocumentWrapper" /> <Binding ElementName="thisPdfViewer" Path="PdfDocumentWrapper" />
<Binding /> <Binding />
<Binding ElementName="thisPdfViewer" />
</MultiBinding> </MultiBinding>
</Image.DataContext> </Image.DataContext>
</Image> </Image>