/// <summary> /// Override the default hash value process to base it on contained values /// </summary> /// <returns>Returns the combined hash code of the properties for identification</returns> public override int GetHashCode() { unchecked { int hash = 17; hash = hash * 31 + (AssociatedType != null ? AssociatedType.GetHashCode() : 0); hash = hash * 31 + UseForChildren.GetHashCode(); return(hash); } }
public void SetConstraint(TemplateConstraint constraint) { if (this.Constraint != null) { throw new InvalidOperationException(); } this.Constraint = constraint ?? TemplateConstraint.Create(NameReference.Create(this.Name), null, null, null, null); this.AssociatedType = TypeDefinition.CreateTypeParameter(this); this.InstanceOf = AssociatedType.GetInstance(overrideMutability: TypeMutability.None, translation: null, lifetime: Lifetime.Timeless); this.attachPostConstructor(); }
public void Attach(DependencyObject dependencyObject) { if (dependencyObject != AssociatedObject) { if (AssociatedObject != null) { throw new InvalidOperationException(Properties.Resources.CannotHostBehaviorMultipleTimesExceptionMessage); } if ((dependencyObject != null) && !AssociatedType.IsAssignableFrom(dependencyObject.GetType())) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.TypeConstraintViolatedExceptionMessage, new object[] { base.GetType().Name, dependencyObject.GetType().Name, AssociatedType.Name })); } base.WritePreamble(); associatedObject = dependencyObject; base.WritePostscript(); OnAssociatedObjectChanged(); OnAttached(); } }
/// <summary> /// This is used to connect to the EventName on the passed target object. /// We wire up to a method in this instance named OnEventRaised. /// </summary> /// <param name="target">Target object</param> /// <param name="eventName">Name of the event to subscribe to</param> void Subscribe(object target, string eventName) { if (target == null || string.IsNullOrEmpty(eventName)) { return; } // Lookup the named event on the associated object. locatedEventInfo = AssociatedType.GetRuntimeEvent(eventName); if (locatedEventInfo == null) { Debug.WriteLine($"EventToCommandBehavior: Event {eventName} not found on {AssociatedType}."); return; } // Wire up the event with reflection. MethodInfo methodInfo = typeof(EventToCommandBehavior).GetTypeInfo().GetDeclaredMethod("OnEventRaised"); Debug.Assert(methodInfo != null); eventHandler = methodInfo.CreateDelegate(locatedEventInfo.EventHandlerType, this); locatedEventInfo.AddEventHandler(target, eventHandler); }