private static void OnEventNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            EventTriggerBehaviorBase behavior = (EventTriggerBehaviorBase)dependencyObject;

            if (behavior.AssociatedObject == null || behavior.resolvedSource == null)
            {
                return;
            }

            string oldEventName = (string)args.OldValue;
            string newEventName = (string)args.NewValue;

            behavior.UnregisterEvent(oldEventName);
            behavior.RegisterEvent(newEventName);
        }
Example #2
0
        private void RegisterEvent(string eventName)
        {
            if (string.IsNullOrEmpty(eventName))
            {
                return;
            }

            if (eventName != "Loaded")
            {
                var sourceObjectType = this.resolvedSource.GetType();
                var info             = sourceObjectType.GetRuntimeEvent(eventName);
                if (info == null)
                {
                    throw new ArgumentException(string.Format(
                                                    "Can not find event {0} for the class {1}",
                                                    this.EventName,
                                                    sourceObjectType.Name));
                }

                var methodInfo = typeof(EventTriggerBehaviorBase).GetTypeInfo().GetDeclaredMethod("OnEvent");
                this.eventHandler = methodInfo.CreateDelegate(info.EventHandlerType, this);

                this.isWindowsRuntimeEvent = EventTriggerBehaviorBase.IsWindowsRuntimeEvent(info);
                if (this.isWindowsRuntimeEvent)
                {
                    this.addEventHandlerMethod    = add => (EventRegistrationToken)info.AddMethod.Invoke(this.resolvedSource, new object[] { add });
                    this.removeEventHandlerMethod = token => info.RemoveMethod.Invoke(this.resolvedSource, new object[] { token });

                    WindowsRuntimeMarshal.AddEventHandler(this.addEventHandlerMethod, this.removeEventHandlerMethod, this.eventHandler);
                }
                else
                {
                    info.AddEventHandler(this.resolvedSource, this.eventHandler);
                }
            }
            else if (!this.isLoadedEventRegistered)
            {
                var element = this.resolvedSource as FrameworkElement;
                if (element != null && !EventTriggerBehaviorBase.IsElementLoaded(element))
                {
                    this.isLoadedEventRegistered = true;
                    element.Loaded += this.OnEvent;
                }
            }
        }
Example #3
0
 private static bool IsWindowsRuntimeEvent(EventInfo eventInfo)
 {
     return(eventInfo != null &&
            EventTriggerBehaviorBase.IsWindowsRuntimeType(eventInfo.EventHandlerType) &&
            EventTriggerBehaviorBase.IsWindowsRuntimeType(eventInfo.DeclaringType));
 }
        private static void OnSourceObjectChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            EventTriggerBehaviorBase behavior = (EventTriggerBehaviorBase)dependencyObject;

            behavior.SetResolvedSource(behavior.ComputeResolvedSource());
        }