From 040c47e01760515fb320b9eded87208ff02091bc Mon Sep 17 00:00:00 2001 From: Bebo-Maker Date: Wed, 23 Mar 2022 08:35:28 +0100 Subject: [PATCH] Fix issue when showing previewer multiple times. Previously when calling ShowPreviewer in the same process multiple times lead to an Exception inside Avalonia. --- QuestPDF.Previewer/DocumentPreviewerExtensions.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/QuestPDF.Previewer/DocumentPreviewerExtensions.cs b/QuestPDF.Previewer/DocumentPreviewerExtensions.cs index 1a335ac..29f3f70 100644 --- a/QuestPDF.Previewer/DocumentPreviewerExtensions.cs +++ b/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()); + return; + } + AppBuilder .Configure(() => new PreviewerApp() {