ActionExecutionContext.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if XFORMS
  2. namespace Caliburn.Micro.Core.Xamarin.Forms
  3. #else
  4. namespace Caliburn.Micro
  5. #endif
  6. {
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. #if WinRT
  11. using Windows.UI.Xaml;
  12. #elif XFORMS
  13. using global::Xamarin.Forms;
  14. using DependencyObject = global::Xamarin.Forms.BindableObject;
  15. using DependencyProperty = global::Xamarin.Forms.BindableProperty;
  16. using FrameworkElement = global::Xamarin.Forms.VisualElement;
  17. #else
  18. using System.Windows;
  19. #endif
  20. /// <summary>
  21. /// The context used during the execution of an Action or its guard.
  22. /// </summary>
  23. public class ActionExecutionContext : IDisposable {
  24. private WeakReference message;
  25. private WeakReference source;
  26. private WeakReference target;
  27. private WeakReference view;
  28. private Dictionary<string, object> values;
  29. /// <summary>
  30. /// Determines whether the action can execute.
  31. /// </summary>
  32. /// <remarks>Returns true if the action can execute, false otherwise.</remarks>
  33. public Func<bool> CanExecute;
  34. /// <summary>
  35. /// Any event arguments associated with the action's invocation.
  36. /// </summary>
  37. public object EventArgs;
  38. /// <summary>
  39. /// The actual method info to be invoked.
  40. /// </summary>
  41. public MethodInfo Method;
  42. /// <summary>
  43. /// The message being executed.
  44. /// </summary>
  45. public ActionMessage Message {
  46. get { return message == null ? null : message.Target as ActionMessage; }
  47. set { message = new WeakReference(value); }
  48. }
  49. /// <summary>
  50. /// The source from which the message originates.
  51. /// </summary>
  52. public FrameworkElement Source {
  53. get { return source == null ? null : source.Target as FrameworkElement; }
  54. set { source = new WeakReference(value); }
  55. }
  56. /// <summary>
  57. /// The instance on which the action is invoked.
  58. /// </summary>
  59. public object Target {
  60. get { return target == null ? null : target.Target; }
  61. set { target = new WeakReference(value); }
  62. }
  63. /// <summary>
  64. /// The view associated with the target.
  65. /// </summary>
  66. public DependencyObject View {
  67. get { return view == null ? null : view.Target as DependencyObject; }
  68. set { view = new WeakReference(value); }
  69. }
  70. /// <summary>
  71. /// Gets or sets additional data needed to invoke the action.
  72. /// </summary>
  73. /// <param name="key">The data key.</param>
  74. /// <returns>Custom data associated with the context.</returns>
  75. public object this[string key] {
  76. get {
  77. if (values == null)
  78. values = new Dictionary<string, object>();
  79. object result;
  80. values.TryGetValue(key, out result);
  81. return result;
  82. }
  83. set {
  84. if (values == null)
  85. values = new Dictionary<string, object>();
  86. values[key] = value;
  87. }
  88. }
  89. /// <summary>
  90. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  91. /// </summary>
  92. public void Dispose() {
  93. Disposing(this, System.EventArgs.Empty);
  94. }
  95. /// <summary>
  96. /// Called when the execution context is disposed
  97. /// </summary>
  98. public event EventHandler Disposing = delegate { };
  99. }
  100. }