public static void AssignEvent(NProperty prop, object action, DependencyObject target, RoutedEvent routedEvent, Func <NEventAdapter, Delegate> extractor) { var handled = NEventAdapter.UnpackHandler(ref action); var agg = (NEventAggregator)target.GetValue(AggregatorProperty); if (agg == null) { target.SetValue(AggregatorProperty, agg = new NEventAggregator()); } var adapter = agg[prop]; if (adapter == null) { agg[prop] = adapter = new NEventAdapter(prop) { Action = action }; var handler = extractor(adapter); var e = target as UIElement; if (e != null) { e.AddHandler(routedEvent, handler, handled); } #if WPF else { var ce = target as ContentElement; if (ce != null) { ce.AddHandler(routedEvent, handler, true); } } #endif } else { adapter.Action = action; } }
public static void AssignEvent <T>(NProperty prop, object action, T target, Action <T, NEventAdapter> subscribe) where T : class { var handled = NEventAdapter.UnpackHandler(ref action); if (handled) { throw new InvalidOperationException($"No support for handled events of {prop.Name}"); } #if XAML var dep = target as DependencyObject; if (dep != null) { var agg = (NEventAggregator)dep.GetValue(AggregatorProperty); if (agg == null) { dep.SetValue(AggregatorProperty, agg = new NEventAggregator()); } var adapter = agg[prop]; if (adapter == null) { agg[prop] = adapter = new NEventAdapter(prop) { Action = action }; subscribe(target, adapter); } else { adapter.Action = action; } } else #elif XFORMS var dep = target as BindableObject; if (dep != null) { var agg = (NEventAggregator)dep.GetValue(AggregatorProperty); if (agg == null) { dep.SetValue(AggregatorProperty, agg = new NEventAggregator()); } var adapter = agg[prop]; if (adapter == null) { agg[prop] = adapter = new NEventAdapter(prop) { Action = action }; subscribe(target, adapter); } else { adapter.Action = action; } } else #endif { subscribe(target, new NEventAdapter(prop) { Action = action }); } }