App.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Styling;
  7. using Avalonia.Themes.Simple;
  8. using Avalonia.Themes.Fluent;
  9. using ControlCatalog.Models;
  10. using ControlCatalog.ViewModels;
  11. namespace ControlCatalog
  12. {
  13. public class App : Application
  14. {
  15. private readonly Styles _themeStylesContainer = new();
  16. private FluentTheme? _fluentTheme;
  17. private SimpleTheme? _simpleTheme;
  18. private IStyle? _colorPickerFluent, _colorPickerSimple;
  19. private IStyle? _dataGridFluent, _dataGridSimple;
  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 = new FluentTheme();
  29. _simpleTheme = new SimpleTheme();
  30. _colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!;
  31. _colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!;
  32. _dataGridFluent = (IStyle)Resources["DataGridFluent"]!;
  33. _dataGridSimple = (IStyle)Resources["DataGridSimple"]!;
  34. SetCatalogThemes(CatalogTheme.Fluent);
  35. }
  36. public override void OnFrameworkInitializationCompleted()
  37. {
  38. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
  39. {
  40. desktopLifetime.MainWindow = new MainWindow { DataContext = new MainWindowViewModel() };
  41. }
  42. else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
  43. {
  44. singleViewLifetime.MainView = new MainView { DataContext = new MainWindowViewModel() };
  45. }
  46. base.OnFrameworkInitializationCompleted();
  47. }
  48. private CatalogTheme _prevTheme;
  49. public static CatalogTheme CurrentTheme => ((App)Current!)._prevTheme;
  50. public static void SetCatalogThemes(CatalogTheme theme)
  51. {
  52. var app = (App)Current!;
  53. var prevTheme = app._prevTheme;
  54. app._prevTheme = theme;
  55. var shouldReopenWindow = prevTheme != theme;
  56. if (app._themeStylesContainer.Count == 0)
  57. {
  58. app._themeStylesContainer.Add(new Style());
  59. app._themeStylesContainer.Add(new Style());
  60. app._themeStylesContainer.Add(new Style());
  61. }
  62. if (theme == CatalogTheme.Fluent)
  63. {
  64. app._themeStylesContainer[0] = app._fluentTheme!;
  65. app._themeStylesContainer[1] = app._colorPickerFluent!;
  66. app._themeStylesContainer[2] = app._dataGridFluent!;
  67. }
  68. else if (theme == CatalogTheme.Simple)
  69. {
  70. app._themeStylesContainer[0] = app._simpleTheme!;
  71. app._themeStylesContainer[1] = app._colorPickerSimple!;
  72. app._themeStylesContainer[2] = app._dataGridSimple!;
  73. }
  74. if (shouldReopenWindow)
  75. {
  76. if (app.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
  77. {
  78. var oldWindow = desktopLifetime.MainWindow;
  79. var newWindow = new MainWindow();
  80. desktopLifetime.MainWindow = newWindow;
  81. newWindow.Show();
  82. oldWindow?.Close();
  83. }
  84. else if (app.ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime)
  85. {
  86. singleViewLifetime.MainView = new MainView();
  87. }
  88. }
  89. }
  90. }
  91. }