Browse Source

refactor initialize in control

donandren 8 years ago
parent
commit
d2f5616142
1 changed files with 23 additions and 22 deletions
  1. 23 22
      src/Avalonia.Controls/Control.cs

+ 23 - 22
src/Avalonia.Controls/Control.cs

@@ -352,18 +352,28 @@ namespace Avalonia.Controls
 
             if (--_initCount == 0 && _isAttachedToLogicalTree)
             {
-                if (!_styled)
-                {
-                    RegisterWithNameScope();
-                    ApplyStyling();
-                    _styled = true;
-                }
+                InitializeStylesIfNeeded();
 
-                if (!IsInitialized)
-                {
-                    IsInitialized = true;
-                    Initialized?.Invoke(this, EventArgs.Empty);
-                }
+                InitializeIfNeeded();
+            }
+        }
+
+        private void InitializeStylesIfNeeded(bool force = false)
+        {
+            if (_initCount == 0 && (!_styled || force))
+            {
+                RegisterWithNameScope();
+                ApplyStyling();
+                _styled = true;
+            }
+        }
+
+        private void InitializeIfNeeded()
+        {
+            if (_initCount == 0 && !IsInitialized)
+            {
+                IsInitialized = true;
+                Initialized?.Invoke(this, EventArgs.Empty);
             }
         }
 
@@ -539,11 +549,7 @@ namespace Avalonia.Controls
         {
             base.OnAttachedToVisualTreeCore(e);
 
-            if (!IsInitialized)
-            {
-                IsInitialized = true;
-                Initialized?.Invoke(this, EventArgs.Empty);
-            }
+            InitializeIfNeeded();
         }
 
         /// <inheritdoc/>
@@ -711,12 +717,7 @@ namespace Avalonia.Controls
             {
                 _isAttachedToLogicalTree = true;
 
-                if (_initCount == 0)
-                {
-                    RegisterWithNameScope();
-                    ApplyStyling();
-                    _styled = true;
-                }
+                InitializeStylesIfNeeded(true);
 
                 OnAttachedToLogicalTree(e);
             }