private static void OnEventNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { EventTriggerBehavior behavior = (EventTriggerBehavior)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); }
private void RegisterEvent(string eventName) { if (string.IsNullOrEmpty(eventName)) { return; } if (eventName != "Loaded") { Type sourceObjectType = this.resolvedSource.GetType(); EventInfo info = sourceObjectType.GetRuntimeEvent(eventName); if (info == null) { throw new ArgumentException(string.Format( CultureInfo.CurrentCulture, ResourceHelper.CannotFindEventNameExceptionMessage, this.EventName, sourceObjectType.Name)); } MethodInfo methodInfo = typeof(EventTriggerBehavior).GetTypeInfo().GetDeclaredMethod("OnEvent"); this.eventHandler = methodInfo.CreateDelegate(info.EventHandlerType, this); this.isWindowsRuntimeEvent = EventTriggerBehavior.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) { FrameworkElement element = this.resolvedSource as FrameworkElement; if (element != null && !EventTriggerBehavior.IsElementLoaded(element)) { this.isLoadedEventRegistered = true; element.Loaded += this.OnEvent; } } }
/// <summary> /// Executes the action. /// </summary> /// <param name="sender">The <see cref="System.Object"/> that is passed to the action by the behavior. Generally this is <seealso cref="Microsoft.Xaml.Interactivity.IBehavior.AssociatedObject"/> or a target object.</param> /// <param name="parameter">The value of this parameter is determined by the caller.</param> /// <returns>True if the transition to the specified state succeeds; else false.</returns> public object Execute(object sender, object parameter) { if (string.IsNullOrEmpty(this.StateName)) { return(false); } if (this.ReadLocalValue(GoToStateAction.TargetObjectProperty) != DependencyProperty.UnsetValue) { Control control = this.TargetObject as Control; if (control == null) { return(false); } return(VisualStateUtilities.GoToState(control, this.StateName, this.UseTransitions)); } FrameworkElement element = sender as FrameworkElement; if (element == null || !EventTriggerBehavior.IsElementLoaded(element)) { return(false); } Control resolvedControl = VisualStateUtilities.FindNearestStatefulControl(element); if (resolvedControl == null) { throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, ResourceHelper.GoToStateActionTargetHasNoStateGroups, element.Name)); } return(VisualStateUtilities.GoToState(resolvedControl, this.StateName, this.UseTransitions)); }
private static bool IsWindowsRuntimeEvent(EventInfo eventInfo) { return(eventInfo != null && EventTriggerBehavior.IsWindowsRuntimeType(eventInfo.EventHandlerType) && EventTriggerBehavior.IsWindowsRuntimeType(eventInfo.DeclaringType)); }
private static void OnSourceObjectChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { EventTriggerBehavior behavior = (EventTriggerBehavior)dependencyObject; behavior.SetResolvedSource(behavior.ComputeResolvedSource()); }