Action.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #if XFORMS
  2. namespace Caliburn.Micro.Core.Xamarin.Forms
  3. #else
  4. namespace Caliburn.Micro
  5. #endif
  6. {
  7. #if WinRT
  8. using System.Linq;
  9. using Windows.UI.Xaml;
  10. using System.Reflection;
  11. #elif XFORMS
  12. using UIElement = global::Xamarin.Forms.Element;
  13. using FrameworkElement = global::Xamarin.Forms.VisualElement;
  14. using DependencyProperty = global::Xamarin.Forms.BindableProperty;
  15. using DependencyObject = global::Xamarin.Forms.BindableObject;
  16. #else
  17. using Caliburn.Micro.Core;
  18. using System.Windows;
  19. #endif
  20. /// <summary>
  21. /// A host for action related attached properties.
  22. /// </summary>
  23. public static class Action {
  24. static readonly ILog Log = LogManager.GetLog(typeof(Action));
  25. /// <summary>
  26. /// A property definition representing the target of an <see cref="ActionMessage" /> . The DataContext of the element will be set to this instance.
  27. /// </summary>
  28. public static readonly DependencyProperty TargetProperty =
  29. DependencyPropertyHelper.RegisterAttached(
  30. "Target",
  31. typeof(object),
  32. typeof(Action),
  33. null,
  34. OnTargetChanged
  35. );
  36. /// <summary>
  37. /// A property definition representing the target of an <see cref="ActionMessage" /> . The DataContext of the element is not set to this instance.
  38. /// </summary>
  39. public static readonly DependencyProperty TargetWithoutContextProperty =
  40. DependencyPropertyHelper.RegisterAttached(
  41. "TargetWithoutContext",
  42. typeof(object),
  43. typeof(Action),
  44. null,
  45. OnTargetWithoutContextChanged
  46. );
  47. /// <summary>
  48. /// Sets the target of the <see cref="ActionMessage" /> .
  49. /// </summary>
  50. /// <param name="d"> The element to attach the target to. </param>
  51. /// <param name="target"> The target for instances of <see cref="ActionMessage" /> . </param>
  52. public static void SetTarget(DependencyObject d, object target) {
  53. d.SetValue(TargetProperty, target);
  54. }
  55. /// <summary>
  56. /// Gets the target for instances of <see cref="ActionMessage" /> .
  57. /// </summary>
  58. /// <param name="d"> The element to which the target is attached. </param>
  59. /// <returns> The target for instances of <see cref="ActionMessage" /> </returns>
  60. public static object GetTarget(DependencyObject d) {
  61. return d.GetValue(TargetProperty);
  62. }
  63. /// <summary>
  64. /// Sets the target of the <see cref="ActionMessage" /> .
  65. /// </summary>
  66. /// <param name="d"> The element to attach the target to. </param>
  67. /// <param name="target"> The target for instances of <see cref="ActionMessage" /> . </param>
  68. /// <remarks>
  69. /// The DataContext will not be set.
  70. /// </remarks>
  71. public static void SetTargetWithoutContext(DependencyObject d, object target) {
  72. d.SetValue(TargetWithoutContextProperty, target);
  73. }
  74. /// <summary>
  75. /// Gets the target for instances of <see cref="ActionMessage" /> .
  76. /// </summary>
  77. /// <param name="d"> The element to which the target is attached. </param>
  78. /// <returns> The target for instances of <see cref="ActionMessage" /> </returns>
  79. public static object GetTargetWithoutContext(DependencyObject d) {
  80. return d.GetValue(TargetWithoutContextProperty);
  81. }
  82. ///<summary>
  83. /// Checks if the <see cref="ActionMessage" /> -Target was set.
  84. ///</summary>
  85. ///<param name="element"> DependencyObject to check </param>
  86. ///<returns> True if Target or TargetWithoutContext was set on <paramref name="element" /> </returns>
  87. public static bool HasTargetSet(DependencyObject element) {
  88. if (GetTarget(element) != null || GetTargetWithoutContext(element) != null)
  89. return true;
  90. #if XFORMS
  91. return false;
  92. #else
  93. var frameworkElement = element as FrameworkElement;
  94. if (frameworkElement == null)
  95. return false;
  96. return ConventionManager.HasBinding(frameworkElement, TargetProperty)
  97. || ConventionManager.HasBinding(frameworkElement, TargetWithoutContextProperty);
  98. #endif
  99. }
  100. #if !XFORMS
  101. ///<summary>
  102. /// Uses the action pipeline to invoke the method.
  103. ///</summary>
  104. ///<param name="target"> The object instance to invoke the method on. </param>
  105. ///<param name="methodName"> The name of the method to invoke. </param>
  106. ///<param name="view"> The view. </param>
  107. ///<param name="source"> The source of the invocation. </param>
  108. ///<param name="eventArgs"> The event args. </param>
  109. ///<param name="parameters"> The method parameters. </param>
  110. public static void Invoke(object target, string methodName, DependencyObject view = null, FrameworkElement source = null, object eventArgs = null, object[] parameters = null) {
  111. var message = new ActionMessage {MethodName = methodName};
  112. var context = new ActionExecutionContext {
  113. Target = target,
  114. #if WinRT
  115. Method = target.GetType().GetRuntimeMethods().Single(m => m.Name == methodName),
  116. #else
  117. Method = target.GetType().GetMethod(methodName),
  118. #endif
  119. Message = message,
  120. View = view,
  121. Source = source,
  122. EventArgs = eventArgs
  123. };
  124. if (parameters != null) {
  125. parameters.Apply(x => context.Message.Parameters.Add(x as Parameter ?? new Parameter { Value = x }));
  126. }
  127. ActionMessage.InvokeAction(context);
  128. // This is a bit of hack but keeps message being garbage collected
  129. Log.Info("Invoking action {0} on {1}.", message.MethodName, target);
  130. }
  131. #endif
  132. static void OnTargetWithoutContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
  133. SetTargetCore(e, d, false);
  134. }
  135. static void OnTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
  136. SetTargetCore(e, d, true);
  137. }
  138. static void SetTargetCore(DependencyPropertyChangedEventArgs e, DependencyObject d, bool setContext) {
  139. if (e.NewValue == e.OldValue || (Execute.InDesignMode && e.NewValue is string)) {
  140. return;
  141. }
  142. var target = e.NewValue;
  143. var containerKey = e.NewValue as string;
  144. if (containerKey != null) {
  145. target = IoC.GetInstance(null, containerKey);
  146. }
  147. #if XFORMS
  148. Log.Info("Attaching message handler {0} to {1}.", target, d);
  149. Message.SetHandler(d, target);
  150. if (setContext && d is FrameworkElement) {
  151. Log.Info("Setting DC of {0} to {1}.", d, target);
  152. ((FrameworkElement)d).BindingContext = target;
  153. }
  154. #else
  155. if (setContext && d is FrameworkElement) {
  156. Log.Info("Setting DC of {0} to {1}.", d, target);
  157. ((FrameworkElement)d).DataContext = target;
  158. }
  159. Log.Info("Attaching message handler {0} to {1}.", target, d);
  160. Message.SetHandler(d, target);
  161. #endif
  162. }
  163. }
  164. }