Browse Source

Lazy initialize Interactive._eventHandlers.

pull/1135/head
Steven Kirk 9 years ago
parent
commit
4bba1ab0fb
  1. 20
      src/Avalonia.Interactivity/Interactive.cs

20
src/Avalonia.Interactivity/Interactive.cs

@ -16,14 +16,18 @@ namespace Avalonia.Interactivity
/// </summary> /// </summary>
public class Interactive : Layoutable, IInteractive public class Interactive : Layoutable, IInteractive
{ {
private readonly Dictionary<RoutedEvent, List<EventSubscription>> _eventHandlers = private Dictionary<RoutedEvent, List<EventSubscription>> _eventHandlers;
new Dictionary<RoutedEvent, List<EventSubscription>>();
/// <summary> /// <summary>
/// Gets the interactive parent of the object for bubbling and tunnelling events. /// Gets the interactive parent of the object for bubbling and tunnelling events.
/// </summary> /// </summary>
IInteractive IInteractive.InteractiveParent => ((IVisual)this).VisualParent as IInteractive; IInteractive IInteractive.InteractiveParent => ((IVisual)this).VisualParent as IInteractive;
private Dictionary<RoutedEvent, List<EventSubscription>> EventHandlers
{
get { return _eventHandlers ?? (_eventHandlers = new Dictionary<RoutedEvent, List<EventSubscription>>()); }
}
/// <summary> /// <summary>
/// Adds a handler for the specified routed event. /// Adds a handler for the specified routed event.
/// </summary> /// </summary>
@ -43,10 +47,10 @@ namespace Avalonia.Interactivity
List<EventSubscription> subscriptions; List<EventSubscription> subscriptions;
if (!_eventHandlers.TryGetValue(routedEvent, out subscriptions)) if (!EventHandlers.TryGetValue(routedEvent, out subscriptions))
{ {
subscriptions = new List<EventSubscription>(); subscriptions = new List<EventSubscription>();
_eventHandlers.Add(routedEvent, subscriptions); EventHandlers.Add(routedEvent, subscriptions);
} }
var sub = new EventSubscription var sub = new EventSubscription
@ -89,9 +93,9 @@ namespace Avalonia.Interactivity
Contract.Requires<ArgumentNullException>(routedEvent != null); Contract.Requires<ArgumentNullException>(routedEvent != null);
Contract.Requires<ArgumentNullException>(handler != null); Contract.Requires<ArgumentNullException>(handler != null);
List<EventSubscription> subscriptions; List<EventSubscription> subscriptions = null;
if (_eventHandlers.TryGetValue(routedEvent, out subscriptions)) if (_eventHandlers?.TryGetValue(routedEvent, out subscriptions) == true)
{ {
subscriptions.RemoveAll(x => x.Handler == handler); subscriptions.RemoveAll(x => x.Handler == handler);
} }
@ -181,9 +185,9 @@ namespace Avalonia.Interactivity
e.RoutedEvent.InvokeRaised(this, e); e.RoutedEvent.InvokeRaised(this, e);
List<EventSubscription> subscriptions; List<EventSubscription> subscriptions = null;
if (_eventHandlers.TryGetValue(e.RoutedEvent, out subscriptions)) if (_eventHandlers?.TryGetValue(e.RoutedEvent, out subscriptions) == true)
{ {
foreach (var sub in subscriptions.ToList()) foreach (var sub in subscriptions.ToList())
{ {

Loading…
Cancel
Save