/// <summary> /// Invokes the action. /// </summary> /// <param name="sender">The Visual Element invoking the event</param> protected override void Invoke(VisualElement sender) { Log.Info("Invoking {0}.", this); if (AssociatedObject == null) { AssociatedObject = sender; } if (context == null) { UpdateContext(); } if (context.Target == null || context.View == null) { PrepareContext(context); if (context.Target == null) { var ex = new Exception(string.Format("No target found for method {0}.", context.Message.MethodName)); Log.Error(ex); if (!ThrowsExceptions) { return; } throw ex; } if (!UpdateAvailabilityCore()) { return; } } if (context.Method == null) { var ex = new Exception(string.Format("Method {0} not found on target of type {1}.", context.Message.MethodName, context.Target.GetType())); Log.Error(ex); if (!ThrowsExceptions) { return; } throw ex; } context.EventArgs = null; // Unfortunately we can't know it :-( if (EnforceGuardsDuringInvocation && context.CanExecute != null && !context.CanExecute()) { return; } InvokeAction(context); context.EventArgs = null; }
public void Execute(object parameter) { if (!string.IsNullOrEmpty(CaliburnAction)) { var methodName = CaliburnAction; if (methodName.EndsWith("")) methodName = methodName.Remove(methodName.Length - 2); // will be overridden by Caliburn. var controls = MetaActionManager.Instance.GetControls(this); var source = controls.First(); var trigger = Parser.Parse(source, CaliburnAction).FirstOrDefault(); var action = trigger.Actions.OfType<ActionMessage>().FirstOrDefault(); var context = new ActionExecutionContext { EventArgs = new EventArgs(), Message = action, Source = source, }; ActionMessage.PrepareContext(context); if (context.CanExecute == null || context.CanExecute()) ActionMessage.InvokeAction(context); } }
/// <summary> /// Invokes the action. /// </summary> /// <param name="eventArgs"> /// The parameter to the action. If the action does not require a parameter, the parameter may be /// set to a null reference. /// </param> protected override void Invoke(object eventArgs) { Log.Info("Invoking {0}.", this); if (context == null) { UpdateContext(); } if (context.Target == null || context.View == null) { PrepareContext(context); if (context.Target == null) { var ex = new Exception(string.Format("No target found for method {0}.", context.Message.MethodName)); Log.Error(ex); if (!ThrowsExceptions) { return; } throw ex; } if (!UpdateAvailabilityCore()) { return; } } if (context.Method == null) { var ex = new Exception(string.Format("Method {0} not found on target of type {1}.", context.Message.MethodName, context.Target.GetType())); Log.Error(ex); if (!ThrowsExceptions) { return; } throw ex; } context.EventArgs = eventArgs; if (EnforceGuardsDuringInvocation && context.CanExecute != null && !context.CanExecute()) { return; } ////实现Aop var attributes = context.Method.GetCustomAttributes(true); if ( attributes.Where(x => x is IPreProcessor) .OrderBy(x => x.GetPropertyValue("PreOrder")) .OfType <IPreProcessor>() .Any(preattribute => !preattribute.Execute(context))) { return; } InvokeAction(context); //实现Aop if ( attributes.Where(x => x is IPostProcessor) .OrderBy(x => x.GetPropertyValue("PostOrder")) .OfType <IPostProcessor>() .Any(postattribute => !postattribute.Execute(context))) { return; } context.EventArgs = null; }