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