MessageControl.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Hardcodet.Wpf.TaskbarNotification;
  15. namespace Aitex.Core.UI.Control
  16. {
  17. /// <summary>
  18. /// Interaction logic for MessageControl.xaml
  19. /// </summary>
  20. public partial class MessageControl : UserControl
  21. {
  22. public MessageControl()
  23. {
  24. InitializeComponent();
  25. this.Visibility = Visibility.Hidden;
  26. }
  27. private bool isClosing = false;
  28. #region BalloonText dependency property
  29. /// <summary>
  30. /// Description
  31. /// </summary>
  32. public static readonly DependencyProperty MessageToolBarText =
  33. DependencyProperty.Register("MessageText",
  34. typeof(string),
  35. typeof(MessageControl),
  36. new FrameworkPropertyMetadata("aaaaa"));
  37. /// <summary>
  38. /// A property wrapper for the <see cref="BalloonTextProperty"/>
  39. /// dependency property:<br/>
  40. /// Description
  41. /// </summary>
  42. public string MessageText
  43. {
  44. get { return (string)GetValue(MessageToolBarText); }
  45. set { SetValue(MessageToolBarText, value); }
  46. }
  47. #endregion
  48. /// <summary>
  49. /// By subscribing to the <see cref="TaskbarIcon.BalloonClosingEvent"/>
  50. /// and setting the "Handled" property to true, we suppress the popup
  51. /// from being closed in order to display the fade-out animation.
  52. /// </summary>
  53. private void OnBalloonClosing(object sender, RoutedEventArgs e)
  54. {
  55. e.Handled = true;
  56. isClosing = true;
  57. }
  58. /// <summary>
  59. /// Resolves the <see cref="TaskbarIcon"/> that displayed
  60. /// the balloon and requests a close action.
  61. /// </summary>
  62. private void imgClose_MouseDown(object sender, MouseButtonEventArgs e)
  63. {
  64. //the tray icon assigned this attached property to simplify access
  65. TaskbarIcon taskbarIcon = TaskbarIcon.GetParentTaskbarIcon(this);
  66. taskbarIcon.CloseBalloon();
  67. }
  68. /// <summary>
  69. /// If the users hovers over the balloon, we don't close it.
  70. /// </summary>
  71. private void grid_MouseEnter(object sender, MouseEventArgs e)
  72. {
  73. //if we're already running the fade-out animation, do not interrupt anymore
  74. //(makes things too complicated for the sample)
  75. if (isClosing) return;
  76. //the tray icon assigned this attached property to simplify access
  77. TaskbarIcon taskbarIcon = TaskbarIcon.GetParentTaskbarIcon(this);
  78. taskbarIcon.ResetBalloonCloseTimer();
  79. }
  80. /// <summary>
  81. /// Closes the popup once the fade-out animation completed.
  82. /// The animation was triggered in XAML through the attached
  83. /// BalloonClosing event.
  84. /// </summary>
  85. private void OnFadeOutCompleted(object sender, EventArgs e)
  86. {
  87. //fix bug
  88. //this will cause next message display fail sometimes
  89. //let taskbaricon class do this job
  90. //Popup pp = (Popup)Parent;
  91. //pp.IsOpen = false;
  92. }
  93. public void SetMessage(MessageType type, string content)
  94. {
  95. switch (type)
  96. {
  97. case MessageType.Info:
  98. MessageLbl.Foreground = Brushes.DarkGreen;
  99. break;
  100. case MessageType.Success:
  101. MessageLbl.Foreground = Brushes.Green;
  102. break;
  103. case MessageType.Warning:
  104. MessageLbl.Foreground = Brushes.Yellow;
  105. break;
  106. case MessageType.Erro:
  107. MessageLbl.Foreground = Brushes.Red;
  108. break;
  109. default:
  110. break;
  111. }
  112. this.Visibility = Visibility.Visible;
  113. //MessageText = @"MessageLbl\r\nMessageLbl";//content;
  114. MessageLbl.Text = content;//@"MessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLblMessageLbl&#x000A;Messag&#13;eLbl";
  115. MessageLbl.ToolTip = content;
  116. }
  117. /// <summary>
  118. /// use to records all the messages
  119. /// </summary>
  120. public static List<string> messageList = new List<string>();
  121. public void ClearMessage()
  122. {
  123. if (!string.IsNullOrEmpty(MessageLbl.Text + ""))
  124. {
  125. messageList.Add(MessageLbl.Text.ToString());
  126. MessageText = "";
  127. }
  128. }
  129. }
  130. }