Browse Source

Fix issue when showing previewer multiple times.

Previously when calling ShowPreviewer  in the same process multiple times lead to an Exception inside Avalonia.
Bebo-Maker 3 years ago
parent
commit
040c47e017
1 changed files with 14 additions and 0 deletions
  1. 14 0
      QuestPDF.Previewer/DocumentPreviewerExtensions.cs

+ 14 - 0
QuestPDF.Previewer/DocumentPreviewerExtensions.cs

@@ -1,4 +1,5 @@
 using Avalonia;
+using Avalonia.Controls.ApplicationLifetimes;
 using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Previewer
@@ -17,6 +18,19 @@ namespace QuestPDF.Previewer
         {
             ArgumentNullException.ThrowIfNull(document);
 
+            //Currently there is no way to unitialize a previously run avalonia app.
+            //So we need to check if the previewer was already run and show the window again.
+            if(Application.Current?.ApplicationLifetime is ClassicDesktopStyleApplicationLifetime desktop)
+            {
+                desktop.MainWindow = new PreviewerWindow()
+                {
+                    Document = document,
+                };
+                desktop.MainWindow.Show();
+                desktop.Start(Array.Empty<string>());
+                return;
+            }
+
             AppBuilder
                 .Configure(() => new PreviewerApp()
                 {