Sfoglia il codice sorgente

Fix null reference exception and fix blank screen when opening a file that can't be read at startup

- Add null checks for `Title` and `CurrentValue` in `WindowFunctions.GetURL` to prevent potential crashes.
- Wrap `MagickImage.Ping` in a try-catch block to handle file reading exceptions and log debug information.
Ruben 3 settimane fa
parent
commit
e0a7fe0fda

+ 11 - 1
src/PicView.Avalonia/StartUp/QuickLoad.cs

@@ -8,6 +8,7 @@ using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.WindowBehavior;
+using PicView.Core.DebugTools;
 using PicView.Core.Exif;
 using PicView.Core.FileHandling;
 using PicView.Core.FileHistory;
@@ -50,7 +51,16 @@ public static class QuickLoad
         }
 
         var magickImage = new MagickImage();
-        magickImage.Ping(fileInfo);
+        try
+        {
+            magickImage.Ping(fileInfo);
+        }
+        catch (Exception e)
+        {
+            // Pinging can lead to crashes when the file cannot be read. 
+            // Just catching the exception here means it will still load correctly regardless
+            DebugHelper.LogDebug(nameof(QuickLoad), nameof(QuickLoadAsync), e);
+        }
         vm.PicViewer.FileInfo.Value = fileInfo;
         var isLargeImage = magickImage.Width * magickImage.Height > 5000000; // ~5 megapixels threshold
         if (isLargeImage || Settings.ImageScaling.ShowImageSideBySide)

+ 1 - 1
src/PicView.Avalonia/WindowBehavior/WindowFunctions.cs

@@ -60,7 +60,7 @@ public static class WindowFunctions
         }
         else
         {
-            var url = vm?.PicViewer.Title.CurrentValue.GetURL();
+            var url = vm?.PicViewer.Title?.CurrentValue?.GetURL();
             lastFile = !string.IsNullOrWhiteSpace(url) ? url : FileHistoryManager.GetLastEntry();
         }