Message.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.Linq;
  10. #if WinRT81
  11. using Windows.UI.Xaml;
  12. using Microsoft.Xaml.Interactivity;
  13. using TriggerBase = Microsoft.Xaml.Interactivity.IBehavior;
  14. #elif XFORMS
  15. using global::Xamarin.Forms;
  16. using UIElement = global::Xamarin.Forms.Element;
  17. using FrameworkElement = global::Xamarin.Forms.VisualElement;
  18. using DependencyProperty = global::Xamarin.Forms.BindableProperty;
  19. using DependencyObject = global::Xamarin.Forms.BindableObject;
  20. #else
  21. using System.Windows;
  22. using System.Windows.Interactivity;
  23. using TriggerBase = System.Windows.Interactivity.TriggerBase;
  24. using Caliburn.Micro.Core;
  25. #endif
  26. /// <summary>
  27. /// Host's attached properties related to routed UI messaging.
  28. /// </summary>
  29. public static class Message {
  30. internal static readonly DependencyProperty HandlerProperty =
  31. DependencyPropertyHelper.RegisterAttached(
  32. "Handler",
  33. typeof(object),
  34. typeof(Message),
  35. null
  36. );
  37. static readonly DependencyProperty MessageTriggersProperty =
  38. DependencyPropertyHelper.RegisterAttached(
  39. "MessageTriggers",
  40. typeof(TriggerBase[]),
  41. typeof(Message),
  42. null
  43. );
  44. /// <summary>
  45. /// Places a message handler on this element.
  46. /// </summary>
  47. /// <param name="d"> The element. </param>
  48. /// <param name="value"> The message handler. </param>
  49. public static void SetHandler(DependencyObject d, object value) {
  50. d.SetValue(HandlerProperty, value);
  51. }
  52. /// <summary>
  53. /// Gets the message handler for this element.
  54. /// </summary>
  55. /// <param name="d"> The element. </param>
  56. /// <returns> The message handler. </returns>
  57. public static object GetHandler(DependencyObject d) {
  58. return d.GetValue(HandlerProperty);
  59. }
  60. /// <summary>
  61. /// A property definition representing attached triggers and messages.
  62. /// </summary>
  63. public static readonly DependencyProperty AttachProperty =
  64. DependencyPropertyHelper.RegisterAttached(
  65. "Attach",
  66. typeof(string),
  67. typeof(Message),
  68. null,
  69. OnAttachChanged
  70. );
  71. /// <summary>
  72. /// Sets the attached triggers and messages.
  73. /// </summary>
  74. /// <param name="d"> The element to attach to. </param>
  75. /// <param name="attachText"> The parsable attachment text. </param>
  76. public static void SetAttach(DependencyObject d, string attachText) {
  77. d.SetValue(AttachProperty, attachText);
  78. }
  79. /// <summary>
  80. /// Gets the attached triggers and messages.
  81. /// </summary>
  82. /// <param name="d"> The element that was attached to. </param>
  83. /// <returns> The parsable attachment text. </returns>
  84. public static string GetAttach(DependencyObject d) {
  85. return d.GetValue(AttachProperty) as string;
  86. }
  87. static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
  88. if (e.NewValue == e.OldValue) {
  89. return;
  90. }
  91. var messageTriggers = (TriggerBase[])d.GetValue(MessageTriggersProperty);
  92. #if WinRT81
  93. var allTriggers = Interaction.GetBehaviors(d);
  94. if (messageTriggers != null)
  95. {
  96. messageTriggers.OfType<DependencyObject>().Apply(x => allTriggers.Remove(x));
  97. }
  98. var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
  99. newTriggers.OfType<DependencyObject>().Apply(allTriggers.Add);
  100. #elif XFORMS
  101. var visualElement = d as VisualElement;
  102. var allTriggers = visualElement != null ? visualElement.Triggers : new List<TriggerBase>();
  103. if (messageTriggers != null) {
  104. messageTriggers.Apply(x => allTriggers.Remove(x));
  105. }
  106. var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
  107. newTriggers.Apply(allTriggers.Add);
  108. #else
  109. var allTriggers = Interaction.GetTriggers(d);
  110. if (messageTriggers != null) {
  111. messageTriggers.Apply(x => allTriggers.Remove(x));
  112. }
  113. var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
  114. newTriggers.Apply(allTriggers.Add);
  115. #endif
  116. if (newTriggers.Length > 0) {
  117. d.SetValue(MessageTriggersProperty, newTriggers);
  118. }
  119. else {
  120. d.ClearValue(MessageTriggersProperty);
  121. }
  122. }
  123. }
  124. }