ViewDView.axaml.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Avalonia.Controls;
  2. using Avalonia.VisualTree;
  3. using YZWater.Avalonia.Controls;
  4. namespace YZWater.Avalonia.Views;
  5. public partial class ViewDView : UserControl
  6. {
  7. private Border? _titleBar;
  8. private Border? _statusBar;
  9. private Border? _rootBorder;
  10. private TextBlock? _titleText;
  11. private TextBlock? _subtitleText;
  12. public ViewDView()
  13. {
  14. InitializeComponent();
  15. AttachedToVisualTree += (_, _) =>
  16. {
  17. FindElements();
  18. ApplyTheme();
  19. ThemeHelper.ThemeChanged += ApplyTheme;
  20. };
  21. DetachedFromVisualTree += (_, _) =>
  22. {
  23. ThemeHelper.ThemeChanged -= ApplyTheme;
  24. };
  25. }
  26. private void FindElements()
  27. {
  28. foreach (var v in this.GetVisualDescendants())
  29. {
  30. if (v is Border b)
  31. {
  32. if (b.Name == "TitleBar") _titleBar = b;
  33. else if (b.Name == "StatusBar") _statusBar = b;
  34. else if (b.Name == "RootBorder") _rootBorder = b;
  35. }
  36. else if (v is TextBlock tb)
  37. {
  38. if (tb.Name == "TitleText") _titleText = tb;
  39. else if (tb.Name == "SubtitleText") _subtitleText = tb;
  40. }
  41. }
  42. }
  43. private void ApplyTheme()
  44. {
  45. if (_titleBar != null) _titleBar.Background = ThemeHelper.HeaderBg;
  46. if (_statusBar != null) _statusBar.Background = ThemeHelper.NavBg;
  47. if (_rootBorder != null) _rootBorder.Background = ThemeHelper.AppBg;
  48. if (_titleText != null) _titleText.Foreground = ThemeHelper.HeaderText;
  49. if (_subtitleText != null) _subtitleText.Foreground = ThemeHelper.HeaderSubtext;
  50. }
  51. }