ソースを参照

Added namescope events.

Steven Kirk 10 年 前
コミット
ad9a2324d0

+ 14 - 0
src/Perspex.Controls/Window.cs

@@ -76,6 +76,20 @@ namespace Perspex.Controls
             _maxPlatformClientSize = this.PlatformImpl.MaxClientSize;
             _maxPlatformClientSize = this.PlatformImpl.MaxClientSize;
         }
         }
 
 
+        /// <inheritdoc/>
+        event EventHandler<NameScopeEventArgs> INameScope.Registered
+        {
+            add { _nameScope.Registered += value; }
+            remove { _nameScope.Registered -= value; }
+        }
+
+        /// <inheritdoc/>
+        event EventHandler<NameScopeEventArgs> INameScope.Unregistered
+        {
+            add { _nameScope.Unregistered += value; }
+            remove { _nameScope.Unregistered -= value; }
+        }
+
         /// <summary>
         /// <summary>
         /// Gets the platform-specific window implementation.
         /// Gets the platform-specific window implementation.
         /// </summary>
         /// </summary>

+ 12 - 0
src/Perspex.SceneGraph/INameScope.cs

@@ -1,6 +1,8 @@
 // Copyright (c) The Perspex Project. All rights reserved.
 // Copyright (c) The Perspex Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
 
+using System;
+
 namespace Perspex
 namespace Perspex
 {
 {
     /// <summary>
     /// <summary>
@@ -8,6 +10,16 @@ namespace Perspex
     /// </summary>
     /// </summary>
     public interface INameScope
     public interface INameScope
     {
     {
+        /// <summary>
+        /// Raised when an element is registered with the name scope.
+        /// </summary>
+        event EventHandler<NameScopeEventArgs> Registered;
+
+        /// <summary>
+        /// Raised when an element is unregistered with the name scope.
+        /// </summary>
+        event EventHandler<NameScopeEventArgs> Unregistered;
+
         /// <summary>
         /// <summary>
         /// Registers an element eith the name scope.
         /// Registers an element eith the name scope.
         /// </summary>
         /// </summary>

+ 18 - 1
src/Perspex.SceneGraph/NameScope.cs

@@ -19,6 +19,16 @@ namespace Perspex
 
 
         private Dictionary<string, object> _inner = new Dictionary<string, object>();
         private Dictionary<string, object> _inner = new Dictionary<string, object>();
 
 
+        /// <summary>
+        /// Raised when an element is registered with the name scope.
+        /// </summary>
+        public event EventHandler<NameScopeEventArgs> Registered;
+
+        /// <summary>
+        /// Raised when an element is unregistered with the name scope.
+        /// </summary>
+        public event EventHandler<NameScopeEventArgs> Unregistered;
+
         /// <summary>
         /// <summary>
         /// Gets the value of the attached <see cref="NameScopeProperty"/> on a visual.
         /// Gets the value of the attached <see cref="NameScopeProperty"/> on a visual.
         /// </summary>
         /// </summary>
@@ -61,6 +71,7 @@ namespace Perspex
             else
             else
             {
             {
                 _inner.Add(name, element);
                 _inner.Add(name, element);
+                Registered?.Invoke(this, new NameScopeEventArgs(name, element));
             }
             }
         }
         }
 
 
@@ -86,7 +97,13 @@ namespace Perspex
         {
         {
             Contract.Requires<ArgumentNullException>(name != null);
             Contract.Requires<ArgumentNullException>(name != null);
 
 
-            _inner.Remove(name);
+            object element;
+
+            if (_inner.TryGetValue(name, out element))
+            {
+                _inner.Remove(name);
+                Registered?.Invoke(this, new NameScopeEventArgs(name, element));
+            }
         }
         }
     }
     }
 }
 }

+ 19 - 0
src/Perspex.SceneGraph/NameScopeEventArgs.cs

@@ -0,0 +1,19 @@
+// Copyright (c) The Perspex Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+using System;
+
+namespace Perspex
+{
+    public class NameScopeEventArgs : EventArgs
+    {
+        public NameScopeEventArgs(string name, object element)
+        {
+            Name = name;
+            Element = element;
+        }
+
+        public string Name { get; }
+        public object Element { get; }
+    }
+}

+ 1 - 0
src/Perspex.SceneGraph/Perspex.SceneGraph.csproj

@@ -103,6 +103,7 @@
     <Compile Include="Media\TileBrush.cs" />
     <Compile Include="Media\TileBrush.cs" />
     <Compile Include="Media\ImageBush.cs" />
     <Compile Include="Media\ImageBush.cs" />
     <Compile Include="Media\VisualBrush.cs" />
     <Compile Include="Media\VisualBrush.cs" />
+    <Compile Include="NameScopeEventArgs.cs" />
     <Compile Include="NameScopeExtensions.cs" />
     <Compile Include="NameScopeExtensions.cs" />
     <Compile Include="NameScope.cs" />
     <Compile Include="NameScope.cs" />
     <Compile Include="RelativePoint.cs" />
     <Compile Include="RelativePoint.cs" />

+ 16 - 4
tests/Perspex.SceneGraph.UnitTests/TestRoot.cs

@@ -9,7 +9,19 @@ namespace Perspex.SceneGraph.UnitTests
 {
 {
     public class TestRoot : TestVisual, IRenderRoot, INameScope
     public class TestRoot : TestVisual, IRenderRoot, INameScope
     {
     {
-        private NameScope nameScope = new NameScope();
+        private NameScope _nameScope = new NameScope();
+
+        event EventHandler<NameScopeEventArgs> INameScope.Registered
+        {
+            add { _nameScope.Registered += value; }
+            remove { _nameScope.Registered -= value; }
+        }
+
+        public event EventHandler<NameScopeEventArgs> Unregistered
+        {
+            add { _nameScope.Unregistered += value; }
+            remove { _nameScope.Unregistered -= value; }
+        }
 
 
         public IRenderTarget RenderTarget
         public IRenderTarget RenderTarget
         {
         {
@@ -28,17 +40,17 @@ namespace Perspex.SceneGraph.UnitTests
 
 
         public void Register(string name, object element)
         public void Register(string name, object element)
         {
         {
-            nameScope.Register(name, element);
+            _nameScope.Register(name, element);
         }
         }
 
 
         public object Find(string name)
         public object Find(string name)
         {
         {
-            return nameScope.Find(name);
+            return _nameScope.Find(name);
         }
         }
 
 
         public void Unregister(string name)
         public void Unregister(string name)
         {
         {
-            nameScope.Unregister(name);
+            _nameScope.Unregister(name);
         }
         }
     }
     }
 }
 }