ToastMessageWindow.xaml.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Timers;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. namespace ivf_tl_Manage.Win
  16. {
  17. /// <summary>
  18. /// ToastMessageWindow.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class ToastMessageWindow : Window
  21. {
  22. private Window parent = null;
  23. public System.Timers.Timer timer;
  24. public ToastMessageWindow(Window w, double viewWidth, double top, string message)
  25. {
  26. InitializeComponent();
  27. this._message.Text = message;
  28. timer = new System.Timers.Timer(1500);
  29. timer.Enabled = false;
  30. timer.AutoReset = false;
  31. timer.Elapsed += timer_Elapsed;
  32. var a = this.ActualWidth;
  33. this.Owner = w;
  34. this.Width = a;
  35. this.parent = w;
  36. Loaded += (a, b) =>
  37. {
  38. this.Top = top;
  39. this.Left = (viewWidth / 2) - (this.ActualWidth / 2);
  40. };
  41. this.Closed += (a, b) =>
  42. {
  43. if (parent != null)
  44. {
  45. parent.Activate();
  46. }
  47. };
  48. timer.Start();
  49. }
  50. public ToastMessageWindow(Window w, string message)
  51. {
  52. InitializeComponent();
  53. this._message.Text = message;
  54. timer = new System.Timers.Timer(1500);
  55. timer.Enabled = false;
  56. timer.AutoReset = false;
  57. timer.Elapsed += timer_Elapsed;
  58. var a = this.ActualWidth;
  59. this.Owner = w;
  60. this.Width = a;
  61. this.parent = w;
  62. this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  63. this.Closed += (a, b) =>
  64. {
  65. if (parent != null)
  66. {
  67. parent.Activate();
  68. }
  69. };
  70. timer.Start();
  71. }
  72. private void timer_Elapsed(object? sender, ElapsedEventArgs e)
  73. {
  74. timer.Stop();
  75. timer.Enabled = false;
  76. timer.Close();
  77. timer.Dispose();
  78. Dispatcher.Invoke(() =>
  79. {
  80. this.Close();
  81. });
  82. }
  83. }
  84. }