PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Caliburn.Micro.Platform/Message.cs

https://github.com/rafaelbm/Caliburn.Micro
C# | 123 lines | 81 code | 16 blank | 26 comment | 9 complexity | bdc71e213f78f36e086fffedcdd864ee MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. namespace Caliburn.Micro {
  2. #if WinRT && !WinRT81
  3. using System.Linq;
  4. using Windows.UI.Xaml;
  5. using Windows.UI.Interactivity;
  6. using TriggerBase = Windows.UI.Interactivity.TriggerBase;
  7. #elif WinRT81
  8. using System.Linq;
  9. using Windows.UI.Xaml;
  10. using Microsoft.Xaml.Interactivity;
  11. using TriggerBase = Microsoft.Xaml.Interactivity.IBehavior;
  12. #else
  13. using System.Linq;
  14. using System.Windows;
  15. using System.Windows.Interactivity;
  16. using TriggerBase = System.Windows.Interactivity.TriggerBase;
  17. #endif
  18. /// <summary>
  19. /// Host's attached properties related to routed UI messaging.
  20. /// </summary>
  21. public static class Message {
  22. internal static readonly DependencyProperty HandlerProperty =
  23. DependencyProperty.RegisterAttached(
  24. "Handler",
  25. typeof(object),
  26. typeof(Message),
  27. null
  28. );
  29. static readonly DependencyProperty MessageTriggersProperty =
  30. DependencyProperty.RegisterAttached(
  31. "MessageTriggers",
  32. typeof(TriggerBase[]),
  33. typeof(Message),
  34. null
  35. );
  36. /// <summary>
  37. /// Places a message handler on this element.
  38. /// </summary>
  39. /// <param name="d"> The element. </param>
  40. /// <param name="value"> The message handler. </param>
  41. public static void SetHandler(DependencyObject d, object value) {
  42. d.SetValue(HandlerProperty, value);
  43. }
  44. /// <summary>
  45. /// Gets the message handler for this element.
  46. /// </summary>
  47. /// <param name="d"> The element. </param>
  48. /// <returns> The message handler. </returns>
  49. public static object GetHandler(DependencyObject d) {
  50. return d.GetValue(HandlerProperty);
  51. }
  52. /// <summary>
  53. /// A property definition representing attached triggers and messages.
  54. /// </summary>
  55. public static readonly DependencyProperty AttachProperty =
  56. DependencyProperty.RegisterAttached(
  57. "Attach",
  58. typeof(string),
  59. typeof(Message),
  60. new PropertyMetadata(null, OnAttachChanged)
  61. );
  62. /// <summary>
  63. /// Sets the attached triggers and messages.
  64. /// </summary>
  65. /// <param name="d"> The element to attach to. </param>
  66. /// <param name="attachText"> The parsable attachment text. </param>
  67. public static void SetAttach(DependencyObject d, string attachText) {
  68. d.SetValue(AttachProperty, attachText);
  69. }
  70. /// <summary>
  71. /// Gets the attached triggers and messages.
  72. /// </summary>
  73. /// <param name="d"> The element that was attached to. </param>
  74. /// <returns> The parsable attachment text. </returns>
  75. public static string GetAttach(DependencyObject d) {
  76. return d.GetValue(AttachProperty) as string;
  77. }
  78. static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
  79. if (e.NewValue == e.OldValue) {
  80. return;
  81. }
  82. var messageTriggers = (TriggerBase[])d.GetValue(MessageTriggersProperty);
  83. #if WinRT81
  84. var allTriggers = Interaction.GetBehaviors(d);
  85. if (messageTriggers != null)
  86. {
  87. messageTriggers.OfType<DependencyObject>().Apply(x => allTriggers.Remove(x));
  88. }
  89. var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
  90. newTriggers.OfType<DependencyObject>().Apply(allTriggers.Add);
  91. #else
  92. var allTriggers = Interaction.GetTriggers(d);
  93. if (messageTriggers != null) {
  94. messageTriggers.Apply(x => allTriggers.Remove(x));
  95. }
  96. var newTriggers = Parser.Parse(d, e.NewValue as string).ToArray();
  97. newTriggers.Apply(allTriggers.Add);
  98. #endif
  99. if (newTriggers.Length > 0) {
  100. d.SetValue(MessageTriggersProperty, newTriggers);
  101. }
  102. else {
  103. d.ClearValue(MessageTriggersProperty);
  104. }
  105. }
  106. }
  107. }