App.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Platform;
  7. using Avalonia.Styling;
  8. using Avalonia.Themes.Simple;
  9. using Avalonia.Themes.Fluent;
  10. using ControlCatalog.Models;
  11. using ControlCatalog.ViewModels;
  12. namespace ControlCatalog
  13. {
  14. public class App : Application
  15. {
  16. private readonly Styles _themeStylesContainer = new();
  17. private FluentTheme? _fluentTheme;
  18. private SimpleTheme? _simpleTheme;
  19. private IStyle? _colorPickerFluent, _colorPickerSimple;
  20. public App()
  21. {
  22. DataContext = new ApplicationViewModel();
  23. }
  24. public override void Initialize()
  25. {
  26. Styles.Add(_themeStylesContainer);
  27. AvaloniaXamlLoader.Load(this);
  28. _fluentTheme = (FluentTheme)Resources["FluentTheme"]!;
  29. _simpleTheme = (SimpleTheme)Resources["SimpleTheme"]!;
  30. _colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!;
  31. _colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!;
  32. SetCatalogThemes(CatalogTheme.Fluent);
  33. }
  34. public override void OnFrameworkInitializationCompleted()
  35. {
  36. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
  37. {
  38. desktopLifetime.MainWindow = new MainWindow { DataContext = new MainWindowViewModel() };
  39. }
  40. else if(ApplicationLifetime is IActivityApplicationLifetime singleViewFactoryApplicationLifetime)
  41. {
  42. singleViewFactoryApplicationLifetime.MainViewFactory = () => new MainView { DataContext = new MainWindowViewModel() };
  43. }
  44. else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
  45. {
  46. singleViewLifetime.MainView = new MainView { DataContext = new MainWindowViewModel() };
  47. }
  48. if (this.TryGetFeature<IActivatableLifetime>() is {} activatableApplicationLifetime)
  49. {
  50. activatableApplicationLifetime.Activated += (sender, args) =>
  51. Console.WriteLine($"App activated: {args.Kind}");
  52. activatableApplicationLifetime.Deactivated += (sender, args) =>
  53. Console.WriteLine($"App deactivated: {args.Kind}");
  54. }
  55. base.OnFrameworkInitializationCompleted();
  56. }
  57. private CatalogTheme _prevTheme;
  58. public static CatalogTheme CurrentTheme => ((App)Current!)._prevTheme;
  59. public static void SetCatalogThemes(CatalogTheme theme)
  60. {
  61. var app = (App)Current!;
  62. var prevTheme = app._prevTheme;
  63. app._prevTheme = theme;
  64. var shouldReopenWindow = prevTheme != theme;
  65. if (app._themeStylesContainer.Count == 0)
  66. {
  67. app._themeStylesContainer.Add(new Style());
  68. app._themeStylesContainer.Add(new Style());
  69. app._themeStylesContainer.Add(new Style());
  70. }
  71. if (theme == CatalogTheme.Fluent)
  72. {
  73. app._themeStylesContainer[0] = app._fluentTheme!;
  74. app._themeStylesContainer[1] = app._colorPickerFluent!;
  75. }
  76. else if (theme == CatalogTheme.Simple)
  77. {
  78. app._themeStylesContainer[0] = app._simpleTheme!;
  79. app._themeStylesContainer[1] = app._colorPickerSimple!;
  80. }
  81. if (shouldReopenWindow)
  82. {
  83. if (app.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
  84. {
  85. var oldWindow = desktopLifetime.MainWindow;
  86. var newWindow = new MainWindow();
  87. desktopLifetime.MainWindow = newWindow;
  88. newWindow.Show();
  89. oldWindow?.Close();
  90. }
  91. else if (app.ApplicationLifetime is IActivityApplicationLifetime singleViewFactoryApplicationLifetime)
  92. {
  93. singleViewFactoryApplicationLifetime.MainViewFactory = () => new MainView { DataContext = new MainWindowViewModel() };
  94. }
  95. else if (app.ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
  96. {
  97. singleViewLifetime.MainView = new MainView();
  98. }
  99. }
  100. }
  101. }
  102. }