终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Toast.xaml.cs 21 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using BPASmartClient.CustomResource.UserControls.Enum;
  2. using BPASmartClient.CustomResource.UserControls.Model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Controls.Primitives;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using System.Windows.Threading;
  19. namespace BPASmartClient.CustomResource.UserControls
  20. {
  21. /// <summary>
  22. /// Toast.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class Toast : UserControl
  25. {
  26. private Window owner = null;
  27. private Popup popup = null;
  28. private DispatcherTimer timer = null;
  29. private Toast()
  30. {
  31. InitializeComponent();
  32. this.DataContext = this;
  33. }
  34. private Toast(Window owner, string message, ToastOptions options = null)
  35. {
  36. Message = message;
  37. InitializeComponent();
  38. if (options != null)
  39. {
  40. if (options.ToastWidth != 0) ToastWidth = options.ToastWidth;
  41. if (options.ToastHeight != 0) ToastHeight = options.ToastHeight;
  42. if (options.TextWidth != 0) TextWidth = options.TextWidth;
  43. Icon = options.Icon;
  44. Location = options.Location;
  45. Time = options.Time;
  46. Closed += options.Closed;
  47. Click += options.Click;
  48. Background = options.Background;
  49. Foreground = options.Foreground;
  50. FontStyle = options.FontStyle;
  51. FontStretch = options.FontStretch;
  52. FontSize = options.FontSize;
  53. FontFamily = options.FontFamily;
  54. FontWeight = options.FontWeight;
  55. IconSize = options.IconSize;
  56. BorderBrush = options.BorderBrush;
  57. BorderThickness = options.BorderThickness;
  58. HorizontalContentAlignment = options.HorizontalContentAlignment;
  59. VerticalContentAlignment = options.VerticalContentAlignment;
  60. CornerRadius = options.CornerRadius;
  61. ToastMargin = options.ToastMargin;
  62. IconForeground = options.IconForeground;
  63. }
  64. this.DataContext = this;
  65. if (owner == null)
  66. {
  67. this.owner = Application.Current.MainWindow;
  68. }
  69. else
  70. {
  71. this.owner = owner;
  72. }
  73. this.owner.Closed += Owner_Closed;
  74. }
  75. private void Owner_Closed(object sender, EventArgs e)
  76. {
  77. this.Close();
  78. }
  79. public static void Show(string msg, ToastOptions options = null)
  80. {
  81. var toast = new Toast(null, msg, options);
  82. int time = toast.Time;
  83. ShowToast(toast, time);
  84. }
  85. public static void Show(Window owner, string msg, ToastOptions options = null)
  86. {
  87. var toast = new Toast(owner, msg, options);
  88. int time = toast.Time;
  89. ShowToast(toast, time);
  90. }
  91. private static void ShowToast(Toast toast, int time)
  92. {
  93. toast.popup = null;
  94. Application.Current.Dispatcher.Invoke(new Action(() =>
  95. {
  96. toast.popup = new Popup
  97. {
  98. PopupAnimation = PopupAnimation.Fade,
  99. AllowsTransparency = true,
  100. StaysOpen = true,
  101. Placement = PlacementMode.Left,
  102. IsOpen = false,
  103. Child = toast,
  104. MinWidth = toast.MinWidth,
  105. MaxWidth = toast.MaxWidth,
  106. MinHeight = toast.MinHeight,
  107. MaxHeight = toast.MaxHeight,
  108. };
  109. if (toast.ToastWidth != 0)
  110. {
  111. toast.popup.Width = toast.ToastWidth;
  112. }
  113. if (toast.ToastHeight != 0)
  114. {
  115. toast.popup.Height = toast.ToastHeight;
  116. }
  117. toast.popup.PlacementTarget = GetPopupPlacementTarget(toast); //为 null 则 Popup 定位相对于屏幕的左上角;
  118. toast.owner.LocationChanged += toast.UpdatePosition;
  119. toast.owner.SizeChanged += toast.UpdatePosition;
  120. toast.popup.Closed += Popup_Closed;
  121. //SetPopupOffset(toast.popup, toast);
  122. //toast.UpdatePosition(toast, null);
  123. toast.popup.IsOpen = true; //先显示出来以确定宽高;
  124. SetPopupOffset(toast.popup, toast);
  125. //toast.UpdatePosition(toast, null);
  126. toast.popup.IsOpen = false; //先关闭再打开来刷新定位;
  127. toast.popup.IsOpen = true;
  128. }));
  129. toast.timer = new DispatcherTimer();
  130. toast.timer.Tick += (sender, e) =>
  131. {
  132. toast.popup.IsOpen = false;
  133. toast.owner.LocationChanged -= toast.UpdatePosition;
  134. toast.owner.SizeChanged -= toast.UpdatePosition;
  135. };
  136. toast.timer.Interval = new TimeSpan(0, 0, 0, 0, time);
  137. toast.timer.Start();
  138. }
  139. private void UpdatePosition(object sender, EventArgs e)
  140. {
  141. var up = typeof(Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  142. if (up == null || popup == null)
  143. {
  144. return;
  145. }
  146. SetPopupOffset(popup, this);
  147. up.Invoke(popup, null);
  148. }
  149. private static void Popup_Closed(object sender, EventArgs e)
  150. {
  151. Popup popup = sender as Popup;
  152. if (popup == null)
  153. {
  154. return;
  155. }
  156. Toast toast = popup.Child as Toast;
  157. if (toast == null)
  158. {
  159. return;
  160. }
  161. toast.RaiseClosed(e);
  162. }
  163. private void UserControl_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  164. {
  165. if (e.ClickCount == 1)
  166. {
  167. RaiseClick(e);
  168. }
  169. }
  170. /// <summary>
  171. /// 获取定位目标
  172. /// </summary>
  173. /// <param name="toast">Toast 对象</param>
  174. /// <returns>容器或null</returns>
  175. private static UIElement GetPopupPlacementTarget(Toast toast)
  176. {
  177. switch (toast.Location)
  178. {
  179. case ToastLocation.ScreenCenter:
  180. case ToastLocation.ScreenLeft:
  181. case ToastLocation.ScreenRight:
  182. case ToastLocation.ScreenTopLeft:
  183. case ToastLocation.ScreenTopCenter:
  184. case ToastLocation.ScreenTopRight:
  185. case ToastLocation.ScreenBottomLeft:
  186. case ToastLocation.ScreenBottomCenter:
  187. case ToastLocation.ScreenBottomRight:
  188. return null;
  189. }
  190. return toast.owner;
  191. }
  192. private static void SetPopupOffset(Popup popup, Toast toast)
  193. {
  194. double winTitleHeight = SystemParameters.CaptionHeight; //标题高度为22;
  195. double owner_width = toast.owner.ActualWidth;
  196. double owner_height = toast.owner.ActualHeight - winTitleHeight;
  197. if (popup.PlacementTarget == null)
  198. {
  199. owner_width = SystemParameters.WorkArea.Size.Width;
  200. owner_height = SystemParameters.WorkArea.Size.Height;
  201. }
  202. double popupWidth = (popup.Child as FrameworkElement)?.ActualWidth ?? 0; //Popup 宽高为其 Child 的宽高;
  203. double popupHeight = (popup.Child as FrameworkElement)?.ActualHeight ?? 0;
  204. double x = SystemParameters.WorkArea.X;
  205. double y = SystemParameters.WorkArea.Y;
  206. Thickness margin = toast.ToastMargin;
  207. /*[dlgcy] 38 和 16 两个数字的猜测:
  208. * PlacementTarget 为 Window 时,当 Placement 为 Bottom 时,Popup 上边缘与 Window 的下边缘的距离为 38;
  209. * 当 Placement 为 Right 时,Popup 左边缘与 Window 的右边缘的距离为 16。
  210. */
  211. double bottomDistance = 38;
  212. double rightDistance = 16;
  213. //上面创建时 Popup 的 Placement 为 PlacementMode.Left;
  214. switch (toast.Location)
  215. {
  216. case ToastLocation.OwnerLeft: //容器左中间
  217. popup.HorizontalOffset = popupWidth + margin.Left;
  218. popup.VerticalOffset = (owner_height - popupHeight - winTitleHeight) / 2;
  219. break;
  220. case ToastLocation.ScreenLeft: //屏幕左中间
  221. popup.HorizontalOffset = popupWidth + x + margin.Left;
  222. popup.VerticalOffset = (owner_height - popupHeight) / 2 + y;
  223. break;
  224. case ToastLocation.OwnerRight: //容器右中间
  225. popup.HorizontalOffset = owner_width - rightDistance - margin.Right;
  226. popup.VerticalOffset = (owner_height - popupHeight - winTitleHeight) / 2;
  227. break;
  228. case ToastLocation.ScreenRight: //屏幕右中间
  229. popup.HorizontalOffset = owner_width + x - margin.Right;
  230. popup.VerticalOffset = (owner_height - popupHeight) / 2 + y;
  231. break;
  232. case ToastLocation.OwnerTopLeft: //容器左上角
  233. popup.HorizontalOffset = popupWidth + margin.Left;
  234. popup.VerticalOffset = margin.Top;
  235. break;
  236. case ToastLocation.ScreenTopLeft: //屏幕左上角
  237. popup.HorizontalOffset = popupWidth + x + margin.Left;
  238. popup.VerticalOffset = margin.Top;
  239. break;
  240. case ToastLocation.OwnerTopCenter: //容器上中间
  241. popup.HorizontalOffset = popupWidth + (owner_width - popupWidth - rightDistance) / 2;
  242. popup.VerticalOffset = margin.Top;
  243. break;
  244. case ToastLocation.ScreenTopCenter: //屏幕上中间
  245. popup.HorizontalOffset = popupWidth + (owner_width - popupWidth) / 2 + x;
  246. popup.VerticalOffset = y + margin.Top;
  247. break;
  248. case ToastLocation.OwnerTopRight: //容器右上角
  249. popup.HorizontalOffset = owner_width - rightDistance - margin.Right;
  250. popup.VerticalOffset = margin.Top;
  251. break;
  252. case ToastLocation.ScreenTopRight: //屏幕右上角
  253. popup.HorizontalOffset = owner_width + x - margin.Right;
  254. popup.VerticalOffset = y + margin.Top;
  255. break;
  256. case ToastLocation.OwnerBottomLeft: //容器左下角
  257. //popup.HorizontalOffset = popupWidth;
  258. //popup.VerticalOffset = owner_height - popupHeight - winTitleHeight;
  259. popup.Placement = PlacementMode.Bottom;
  260. popup.HorizontalOffset = margin.Left;
  261. popup.VerticalOffset = -(bottomDistance + popupHeight + margin.Bottom);
  262. break;
  263. case ToastLocation.ScreenBottomLeft: //屏幕左下角
  264. popup.HorizontalOffset = popupWidth + x + margin.Left;
  265. popup.VerticalOffset = owner_height - popupHeight + y - margin.Bottom;
  266. break;
  267. case ToastLocation.OwnerBottomCenter: //容器下中间
  268. //popup.HorizontalOffset = popupWidth + (owner_width - popupWidth - rightDistance) / 2;
  269. //popup.VerticalOffset = owner_height - popupHeight - winTitleHeight;
  270. popup.Placement = PlacementMode.Bottom;
  271. popup.HorizontalOffset = (owner_width - popupWidth - rightDistance) / 2;
  272. popup.VerticalOffset = -(bottomDistance + popupHeight + margin.Bottom);
  273. break;
  274. case ToastLocation.ScreenBottomCenter: //屏幕下中间
  275. popup.HorizontalOffset = popupWidth + (owner_width - popupWidth) / 2 + x;
  276. popup.VerticalOffset = owner_height - popupHeight + y - margin.Bottom;
  277. break;
  278. case ToastLocation.OwnerBottomRight: //容器右下角
  279. //popup.HorizontalOffset = popupWidth + (owner_width - popupWidth - rightDistance);
  280. //popup.VerticalOffset = owner_height - popupHeight - winTitleHeight;
  281. popup.Placement = PlacementMode.Bottom;
  282. popup.HorizontalOffset = owner_width - popupWidth - rightDistance - margin.Right;
  283. popup.VerticalOffset = -(bottomDistance + popupHeight + margin.Bottom);
  284. break;
  285. case ToastLocation.ScreenBottomRight: //屏幕右下角
  286. popup.HorizontalOffset = owner_width + x - margin.Right;
  287. popup.VerticalOffset = owner_height - popupHeight + y - margin.Bottom;
  288. break;
  289. case ToastLocation.ScreenCenter: //屏幕正中间
  290. popup.HorizontalOffset = popupWidth + (owner_width - popupWidth) / 2 + x;
  291. popup.VerticalOffset = (owner_height - popupHeight) / 2 + y;
  292. break;
  293. case ToastLocation.OwnerCenter: //容器正中间
  294. case ToastLocation.Default:
  295. //popup.HorizontalOffset = popupWidth + (owner_width - popupWidth - rightDistance) / 2;
  296. //popup.VerticalOffset = (owner_height - popupHeight - winTitleHeight) / 2;
  297. popup.Placement = PlacementMode.Center;
  298. popup.HorizontalOffset = -rightDistance / 2;
  299. popup.VerticalOffset = -bottomDistance / 2;
  300. break;
  301. }
  302. }
  303. public void Close()
  304. {
  305. if (timer != null)
  306. {
  307. timer.Stop();
  308. timer = null;
  309. }
  310. popup.IsOpen = false;
  311. owner.LocationChanged -= UpdatePosition;
  312. owner.SizeChanged -= UpdatePosition;
  313. }
  314. private event EventHandler<EventArgs> Closed;
  315. private void RaiseClosed(EventArgs e)
  316. {
  317. Closed?.Invoke(this, e);
  318. }
  319. private event EventHandler<EventArgs> Click;
  320. private void RaiseClick(EventArgs e)
  321. {
  322. Click?.Invoke(this, e);
  323. }
  324. #region 依赖属性
  325. private string Message
  326. {
  327. get { return (string)GetValue(MessageProperty); }
  328. set { SetValue(MessageProperty, value); }
  329. }
  330. private static readonly DependencyProperty MessageProperty =
  331. DependencyProperty.Register("Message", typeof(string), typeof(Toast), new PropertyMetadata(string.Empty));
  332. private CornerRadius CornerRadius
  333. {
  334. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  335. set { SetValue(CornerRadiusProperty, value); }
  336. }
  337. private static readonly DependencyProperty CornerRadiusProperty =
  338. DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(Toast), new PropertyMetadata(new CornerRadius(5)));
  339. private double IconSize
  340. {
  341. get { return (double)GetValue(IconSizeProperty); }
  342. set { SetValue(IconSizeProperty, value); }
  343. }
  344. private static readonly DependencyProperty IconSizeProperty =
  345. DependencyProperty.Register("IconSize", typeof(double), typeof(Toast), new PropertyMetadata(26.0));
  346. private new Brush BorderBrush
  347. {
  348. get { return (Brush)GetValue(BorderBrushProperty); }
  349. set { SetValue(BorderBrushProperty, value); }
  350. }
  351. private static new readonly DependencyProperty BorderBrushProperty =
  352. DependencyProperty.Register("BorderBrush", typeof(Brush), typeof(Toast), new PropertyMetadata((Brush)new BrushConverter().ConvertFromString("#FFFFFF")));
  353. private new Thickness BorderThickness
  354. {
  355. get { return (Thickness)GetValue(BorderThicknessProperty); }
  356. set { SetValue(BorderThicknessProperty, value); }
  357. }
  358. private static new readonly DependencyProperty BorderThicknessProperty =
  359. DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(Toast), new PropertyMetadata(new Thickness(0)));
  360. private new Brush Background
  361. {
  362. get { return (Brush)GetValue(BackgroundProperty); }
  363. set { SetValue(BackgroundProperty, value); }
  364. }
  365. private static new readonly DependencyProperty BackgroundProperty =
  366. DependencyProperty.Register("Background", typeof(Brush), typeof(Toast), new PropertyMetadata((Brush)new BrushConverter().ConvertFromString("#2E2929")));
  367. private new HorizontalAlignment HorizontalContentAlignment
  368. {
  369. get { return (HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty); }
  370. set { SetValue(HorizontalContentAlignmentProperty, value); }
  371. }
  372. private static new readonly DependencyProperty HorizontalContentAlignmentProperty =
  373. DependencyProperty.Register("HorizontalContentAlignment", typeof(HorizontalAlignment), typeof(Toast), new PropertyMetadata(HorizontalAlignment.Left));
  374. private new VerticalAlignment VerticalContentAlignment
  375. {
  376. get { return (VerticalAlignment)GetValue(VerticalContentAlignmentProperty); }
  377. set { SetValue(VerticalContentAlignmentProperty, value); }
  378. }
  379. private static new readonly DependencyProperty VerticalContentAlignmentProperty =
  380. DependencyProperty.Register("VerticalContentAlignment", typeof(VerticalAlignment), typeof(Toast), new PropertyMetadata(VerticalAlignment.Center));
  381. private double ToastWidth
  382. {
  383. get { return (double)GetValue(ToastWidthProperty); }
  384. set { SetValue(ToastWidthProperty, value); }
  385. }
  386. private static readonly DependencyProperty ToastWidthProperty =
  387. DependencyProperty.Register("ToastWidth", typeof(double), typeof(Toast), new PropertyMetadata(0.0));
  388. private double ToastHeight
  389. {
  390. get { return (double)GetValue(ToastHeightProperty); }
  391. set { SetValue(ToastHeightProperty, value); }
  392. }
  393. private static readonly DependencyProperty ToastHeightProperty =
  394. DependencyProperty.Register("ToastHeight", typeof(double), typeof(Toast), new PropertyMetadata(0.0));
  395. private ToastIcons Icon
  396. {
  397. get { return (ToastIcons)GetValue(IconProperty); }
  398. set { SetValue(IconProperty, value); }
  399. }
  400. private static readonly DependencyProperty IconProperty =
  401. DependencyProperty.Register("Icon", typeof(ToastIcons), typeof(Toast), new PropertyMetadata(ToastIcons.None));
  402. private int Time
  403. {
  404. get { return (int)GetValue(TimeProperty); }
  405. set { SetValue(TimeProperty, value); }
  406. }
  407. private static readonly DependencyProperty TimeProperty =
  408. DependencyProperty.Register("Time", typeof(int), typeof(Toast), new PropertyMetadata(2000));
  409. private ToastLocation Location
  410. {
  411. get { return (ToastLocation)GetValue(LocationProperty); }
  412. set { SetValue(LocationProperty, value); }
  413. }
  414. private static readonly DependencyProperty LocationProperty =
  415. DependencyProperty.Register("Location", typeof(ToastLocation), typeof(Toast), new PropertyMetadata(ToastLocation.Default));
  416. public double TextWidth
  417. {
  418. get { return (double)GetValue(TextWidthProperty); }
  419. set { SetValue(TextWidthProperty, value); }
  420. }
  421. public static readonly DependencyProperty TextWidthProperty =
  422. DependencyProperty.Register("TextWidth", typeof(double), typeof(Toast), new PropertyMetadata(double.NaN));
  423. public Thickness ToastMargin
  424. {
  425. get { return (Thickness)GetValue(ToastMarginProperty); }
  426. set { SetValue(ToastMarginProperty, value); }
  427. }
  428. public static readonly DependencyProperty ToastMarginProperty =
  429. DependencyProperty.Register("ToastMargin", typeof(Thickness), typeof(Toast), new PropertyMetadata(new Thickness(0)));
  430. private Brush IconForeground
  431. {
  432. get { return (Brush)GetValue(IconForegroundProperty); }
  433. set { SetValue(IconForegroundProperty, value); }
  434. }
  435. private static readonly DependencyProperty IconForegroundProperty =
  436. DependencyProperty.Register("IconForeground", typeof(Brush), typeof(Toast), new PropertyMetadata((Brush)new BrushConverter().ConvertFromString("#00D91A")));
  437. #endregion
  438. }
  439. }