DocumentPreviewerExtensions.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.ReactiveUI;
  4. using QuestPDF.Infrastructure;
  5. namespace QuestPDF.Previewer
  6. {
  7. public static class DocumentPreviewerExtensions
  8. {
  9. /// <summary>
  10. /// Opens document in the QuestPDF previewer tool.
  11. /// Improves development speed by supporting hot reloading.
  12. /// Shows document preview and refreshes it after each code change.
  13. /// </summary>
  14. /// <remarks>
  15. /// Intended for development only. Do not use in production environment.
  16. /// </remarks>
  17. public static void ShowInPreviewer(this IDocument document)
  18. {
  19. ArgumentNullException.ThrowIfNull(document);
  20. // currently there is no way to utilize a previously run Avalonia app.
  21. // so we need to check if the previewer was already run and show the window again.
  22. if(Application.Current?.ApplicationLifetime is ClassicDesktopStyleApplicationLifetime desktop)
  23. {
  24. desktop.MainWindow = new PreviewerWindow()
  25. {
  26. DataContext = new PreviewerWindowViewModel()
  27. {
  28. Document = document,
  29. }
  30. };
  31. desktop.MainWindow.Show();
  32. desktop.Start(Array.Empty<string>());
  33. return;
  34. }
  35. AppBuilder
  36. .Configure(() => new PreviewerApp()
  37. {
  38. Document = document,
  39. })
  40. .UsePlatformDetect()
  41. .UseReactiveUI()
  42. .StartWithClassicDesktopLifetime(Array.Empty<string>());
  43. }
  44. }
  45. }