P膩rl奴kot izejas kodu

淇鏍囬鏍忎富棰 - 鐢ˋttachedToVisualTree纭繚瑙嗚鏍戝凡灏辩华

纾 鏇 6 dienas atpaka募
vec膩ks
rev墨zija
ff0b5a0832

+ 9 - 3
src/YZWater.Avalonia/Views/ViewAView.axaml.cs

@@ -9,13 +9,19 @@ public partial class ViewAView : UserControl
     public ViewAView()
     {
         InitializeComponent();
-        ThemeHelper.ThemeChanged += ApplyTheme;
-        ApplyTheme();
+        AttachedToVisualTree += (_, _) =>
+        {
+            ApplyTheme();
+            ThemeHelper.ThemeChanged += ApplyTheme;
+        };
+        DetachedFromVisualTree += (_, _) =>
+        {
+            ThemeHelper.ThemeChanged -= ApplyTheme;
+        };
     }
 
     private void ApplyTheme()
     {
-        // 閬嶅巻瑙嗚鏍戞壘鍒板懡鍚嶅厓绱
         foreach (var visual in this.GetVisualDescendants())
         {
             if (visual is Border border && border.Name == "TitleBar")

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

@@ -9,8 +9,15 @@ public partial class ViewBView : UserControl
     public ViewBView()
     {
         InitializeComponent();
-        ThemeHelper.ThemeChanged += ApplyTheme;
-        ApplyTheme();
+        AttachedToVisualTree += (_, _) =>
+        {
+            ApplyTheme();
+            ThemeHelper.ThemeChanged += ApplyTheme;
+        };
+        DetachedFromVisualTree += (_, _) =>
+        {
+            ThemeHelper.ThemeChanged -= ApplyTheme;
+        };
     }
 
     private void ApplyTheme()

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

@@ -9,8 +9,15 @@ public partial class ViewCView : UserControl
     public ViewCView()
     {
         InitializeComponent();
-        ThemeHelper.ThemeChanged += ApplyTheme;
-        ApplyTheme();
+        AttachedToVisualTree += (_, _) =>
+        {
+            ApplyTheme();
+            ThemeHelper.ThemeChanged += ApplyTheme;
+        };
+        DetachedFromVisualTree += (_, _) =>
+        {
+            ThemeHelper.ThemeChanged -= ApplyTheme;
+        };
     }
 
     private void ApplyTheme()

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

@@ -9,8 +9,15 @@ public partial class ViewDView : UserControl
     public ViewDView()
     {
         InitializeComponent();
-        ThemeHelper.ThemeChanged += ApplyTheme;
-        ApplyTheme();
+        AttachedToVisualTree += (_, _) =>
+        {
+            ApplyTheme();
+            ThemeHelper.ThemeChanged += ApplyTheme;
+        };
+        DetachedFromVisualTree += (_, _) =>
+        {
+            ThemeHelper.ThemeChanged -= ApplyTheme;
+        };
     }
 
     private void ApplyTheme()

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

@@ -9,8 +9,15 @@ public partial class ViewEView : UserControl
     public ViewEView()
     {
         InitializeComponent();
-        ThemeHelper.ThemeChanged += ApplyTheme;
-        ApplyTheme();
+        AttachedToVisualTree += (_, _) =>
+        {
+            ApplyTheme();
+            ThemeHelper.ThemeChanged += ApplyTheme;
+        };
+        DetachedFromVisualTree += (_, _) =>
+        {
+            ThemeHelper.ThemeChanged -= ApplyTheme;
+        };
     }
 
     private void ApplyTheme()