using Avalonia.Controls; using Avalonia.VisualTree; using YZWater.Avalonia.Controls; namespace YZWater.Avalonia.Views; public partial class ViewDView : UserControl { private Border? _titleBar; private Border? _statusBar; private Border? _rootBorder; private TextBlock? _titleText; private TextBlock? _subtitleText; public ViewDView() { InitializeComponent(); AttachedToVisualTree += (_, _) => { FindElements(); ApplyTheme(); ThemeHelper.ThemeChanged += ApplyTheme; }; DetachedFromVisualTree += (_, _) => { ThemeHelper.ThemeChanged -= ApplyTheme; }; } private void FindElements() { foreach (var v in this.GetVisualDescendants()) { if (v is Border b) { if (b.Name == "TitleBar") _titleBar = b; else if (b.Name == "StatusBar") _statusBar = b; else if (b.Name == "RootBorder") _rootBorder = b; } else if (v is TextBlock tb) { if (tb.Name == "TitleText") _titleText = tb; else if (tb.Name == "SubtitleText") _subtitleText = tb; } } } private void ApplyTheme() { if (_titleBar != null) _titleBar.Background = ThemeHelper.HeaderBg; if (_statusBar != null) _statusBar.Background = ThemeHelper.NavBg; if (_rootBorder != null) _rootBorder.Background = ThemeHelper.AppBg; if (_titleText != null) _titleText.Foreground = ThemeHelper.HeaderText; if (_subtitleText != null) _subtitleText.Foreground = ThemeHelper.HeaderSubtext; } }