1
0
协褏 褋褍褉胁邪谢卸懈泄谐 褏邪褉邪褏

淇瀛愯鍥炬爣棰樻爮 - 鐢↙oaded+FindControl鏇夸唬GetVisualDescendants

纾 鏇 6 萤写萤褉 萤屑薪萤
parent
commit
79d5ca2664

+ 11 - 12
src/YZWater.Avalonia/Views/ViewAView.axaml.cs

@@ -1,4 +1,5 @@
 using Avalonia.Controls;
+using Avalonia.Media;
 using Avalonia.VisualTree;
 using YZWater.Avalonia.Controls;
 
@@ -9,24 +10,22 @@ public partial class ViewAView : UserControl
     public ViewAView()
     {
         InitializeComponent();
-        AttachedToVisualTree += (_, _) =>
+        Loaded += (_, _) =>
         {
-            foreach (var v in this.GetVisualDescendants())
-            {
-                if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-                else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-            }
+            ApplyTheme();
             ThemeHelper.ThemeChanged += ApplyTheme;
         };
-        DetachedFromVisualTree += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
+        Unloaded += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
     }
 
     private void ApplyTheme()
     {
-        foreach (var v in this.GetVisualDescendants())
-        {
-            if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-            else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-        }
+        // 鐩存帴閫氳繃鍚嶇О鏌ユ壘鍏冪礌
+        if (this.FindControl<Border>("TitleBar") is Border titleBar)
+            titleBar.Background = ThemeHelper.HeaderBg;
+        if (this.FindControl<Border>("StatusBar") is Border statusBar)
+            statusBar.Background = ThemeHelper.NavBg;
+        if (this.FindControl<Border>("RootBorder") is Border root)
+            root.Background = ThemeHelper.AppBg;
     }
 }

+ 9 - 13
src/YZWater.Avalonia/Views/ViewBView.axaml.cs

@@ -1,5 +1,4 @@
 using Avalonia.Controls;
-using Avalonia.VisualTree;
 using YZWater.Avalonia.Controls;
 
 namespace YZWater.Avalonia.Views;
@@ -9,24 +8,21 @@ public partial class ViewBView : UserControl
     public ViewBView()
     {
         InitializeComponent();
-        AttachedToVisualTree += (_, _) =>
+        Loaded += (_, _) =>
         {
-            foreach (var v in this.GetVisualDescendants())
-            {
-                if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-                else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-            }
+            ApplyTheme();
             ThemeHelper.ThemeChanged += ApplyTheme;
         };
-        DetachedFromVisualTree += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
+        Unloaded += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
     }
 
     private void ApplyTheme()
     {
-        foreach (var v in this.GetVisualDescendants())
-        {
-            if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-            else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-        }
+        if (this.FindControl<Border>("TitleBar") is Border titleBar)
+            titleBar.Background = ThemeHelper.HeaderBg;
+        if (this.FindControl<Border>("StatusBar") is Border statusBar)
+            statusBar.Background = ThemeHelper.NavBg;
+        if (this.FindControl<Border>("RootBorder") is Border root)
+            root.Background = ThemeHelper.AppBg;
     }
 }

+ 9 - 13
src/YZWater.Avalonia/Views/ViewCView.axaml.cs

@@ -1,5 +1,4 @@
 using Avalonia.Controls;
-using Avalonia.VisualTree;
 using YZWater.Avalonia.Controls;
 
 namespace YZWater.Avalonia.Views;
@@ -9,24 +8,21 @@ public partial class ViewCView : UserControl
     public ViewCView()
     {
         InitializeComponent();
-        AttachedToVisualTree += (_, _) =>
+        Loaded += (_, _) =>
         {
-            foreach (var v in this.GetVisualDescendants())
-            {
-                if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-                else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-            }
+            ApplyTheme();
             ThemeHelper.ThemeChanged += ApplyTheme;
         };
-        DetachedFromVisualTree += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
+        Unloaded += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
     }
 
     private void ApplyTheme()
     {
-        foreach (var v in this.GetVisualDescendants())
-        {
-            if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-            else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-        }
+        if (this.FindControl<Border>("TitleBar") is Border titleBar)
+            titleBar.Background = ThemeHelper.HeaderBg;
+        if (this.FindControl<Border>("StatusBar") is Border statusBar)
+            statusBar.Background = ThemeHelper.NavBg;
+        if (this.FindControl<Border>("RootBorder") is Border root)
+            root.Background = ThemeHelper.AppBg;
     }
 }

+ 9 - 13
src/YZWater.Avalonia/Views/ViewDView.axaml.cs

@@ -1,5 +1,4 @@
 using Avalonia.Controls;
-using Avalonia.VisualTree;
 using YZWater.Avalonia.Controls;
 
 namespace YZWater.Avalonia.Views;
@@ -9,24 +8,21 @@ public partial class ViewDView : UserControl
     public ViewDView()
     {
         InitializeComponent();
-        AttachedToVisualTree += (_, _) =>
+        Loaded += (_, _) =>
         {
-            foreach (var v in this.GetVisualDescendants())
-            {
-                if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-                else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-            }
+            ApplyTheme();
             ThemeHelper.ThemeChanged += ApplyTheme;
         };
-        DetachedFromVisualTree += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
+        Unloaded += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
     }
 
     private void ApplyTheme()
     {
-        foreach (var v in this.GetVisualDescendants())
-        {
-            if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-            else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-        }
+        if (this.FindControl<Border>("TitleBar") is Border titleBar)
+            titleBar.Background = ThemeHelper.HeaderBg;
+        if (this.FindControl<Border>("StatusBar") is Border statusBar)
+            statusBar.Background = ThemeHelper.NavBg;
+        if (this.FindControl<Border>("RootBorder") is Border root)
+            root.Background = ThemeHelper.AppBg;
     }
 }

+ 9 - 13
src/YZWater.Avalonia/Views/ViewEView.axaml.cs

@@ -1,5 +1,4 @@
 using Avalonia.Controls;
-using Avalonia.VisualTree;
 using YZWater.Avalonia.Controls;
 
 namespace YZWater.Avalonia.Views;
@@ -9,24 +8,21 @@ public partial class ViewEView : UserControl
     public ViewEView()
     {
         InitializeComponent();
-        AttachedToVisualTree += (_, _) =>
+        Loaded += (_, _) =>
         {
-            foreach (var v in this.GetVisualDescendants())
-            {
-                if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-                else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-            }
+            ApplyTheme();
             ThemeHelper.ThemeChanged += ApplyTheme;
         };
-        DetachedFromVisualTree += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
+        Unloaded += (_, _) => ThemeHelper.ThemeChanged -= ApplyTheme;
     }
 
     private void ApplyTheme()
     {
-        foreach (var v in this.GetVisualDescendants())
-        {
-            if (v is Border b && b.Name == "TitleBar") b.Background = ThemeHelper.HeaderBg;
-            else if (v is Border b2 && b2.Name == "StatusBar") b2.Background = ThemeHelper.NavBg;
-        }
+        if (this.FindControl<Border>("TitleBar") is Border titleBar)
+            titleBar.Background = ThemeHelper.HeaderBg;
+        if (this.FindControl<Border>("StatusBar") is Border statusBar)
+            statusBar.Background = ThemeHelper.NavBg;
+        if (this.FindControl<Border>("RootBorder") is Border root)
+            root.Background = ThemeHelper.AppBg;
     }
 }