App.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. private IStyle? _dataGridFluent, _dataGridSimple;
  21. public App()
  22. {
  23. DataContext = new ApplicationViewModel();
  24. }
  25. public override void Initialize()
  26. {
  27. Styles.Add(_themeStylesContainer);
  28. AvaloniaXamlLoader.Load(this);
  29. _fluentTheme = (FluentTheme)Resources["FluentTheme"]!;
  30. _simpleTheme = (SimpleTheme)Resources["SimpleTheme"]!;
  31. _colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!;
  32. _colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!;
  33. _dataGridFluent = (IStyle)Resources["DataGridFluent"]!;
  34. _dataGridSimple = (IStyle)Resources["DataGridSimple"]!;
  35. SetCatalogThemes(CatalogTheme.Fluent);
  36. }
  37. public override void OnFrameworkInitializationCompleted()
  38. {
  39. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
  40. {
  41. desktopLifetime.MainWindow = new MainWindow { DataContext = new MainWindowViewModel() };
  42. }
  43. else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
  44. {
  45. singleViewLifetime.MainView = new MainView { DataContext = new MainWindowViewModel() };
  46. }
  47. if (this.TryGetFeature<IActivatableLifetime>() is {} activatableApplicationLifetime)
  48. {
  49. activatableApplicationLifetime.Activated += (sender, args) =>
  50. Console.WriteLine($"App activated: {args.Kind}");
  51. activatableApplicationLifetime.Deactivated += (sender, args) =>
  52. Console.WriteLine($"App deactivated: {args.Kind}");
  53. }
  54. base.OnFrameworkInitializationCompleted();
  55. }
  56. private CatalogTheme _prevTheme;
  57. public static CatalogTheme CurrentTheme => ((App)Current!)._prevTheme;
  58. public static void SetCatalogThemes(CatalogTheme theme)
  59. {
  60. var app = (App)Current!;
  61. var prevTheme = app._prevTheme;
  62. app._prevTheme = theme;
  63. var shouldReopenWindow = prevTheme != theme;
  64. if (app._themeStylesContainer.Count == 0)
  65. {
  66. app._themeStylesContainer.Add(new Style());
  67. app._themeStylesContainer.Add(new Style());
  68. app._themeStylesContainer.Add(new Style());
  69. }
  70. if (theme == CatalogTheme.Fluent)
  71. {
  72. app._themeStylesContainer[0] = app._fluentTheme!;
  73. app._themeStylesContainer[1] = app._colorPickerFluent!;
  74. app._themeStylesContainer[2] = app._dataGridFluent!;
  75. }
  76. else if (theme == CatalogTheme.Simple)
  77. {
  78. app._themeStylesContainer[0] = app._simpleTheme!;
  79. app._themeStylesContainer[1] = app._colorPickerSimple!;
  80. app._themeStylesContainer[2] = app._dataGridSimple!;
  81. }
  82. if (shouldReopenWindow)
  83. {
  84. if (app.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
  85. {
  86. var oldWindow = desktopLifetime.MainWindow;
  87. var newWindow = new MainWindow();
  88. desktopLifetime.MainWindow = newWindow;
  89. newWindow.Show();
  90. oldWindow?.Close();
  91. }
  92. else if (app.ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
  93. {
  94. singleViewLifetime.MainView = new MainView();
  95. }
  96. }
  97. }
  98. }
  99. }