TrayController.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using EEMSUIClient.Views;
  2. using Hardcodet.Wpf.TaskbarNotification;
  3. using System.Windows;
  4. namespace EEMSUIClient.Services;
  5. public sealed class TrayController : ITrayControl
  6. {
  7. private TaskbarIcon? _tray;
  8. private bool disposedValue;
  9. public TaskbarIcon? Tray
  10. {
  11. set
  12. {
  13. ObjectDisposedException.ThrowIf(disposedValue, nameof(_tray));
  14. _tray = value;
  15. }
  16. }
  17. public bool IsExiting { get; private set; }
  18. public void HideToTray()
  19. {
  20. var mainWindow = (MainWindow)Application.Current.MainWindow;
  21. mainWindow.ShowInTaskbar = false;
  22. mainWindow.Hide();
  23. }
  24. public void ShowMainWindow()
  25. {
  26. var mainWindow = (MainWindow)Application.Current.MainWindow;
  27. if (!mainWindow.IsVisible)
  28. {
  29. mainWindow.Show();
  30. }
  31. mainWindow.WindowState = WindowState.Normal;
  32. mainWindow.ShowInTaskbar = true;
  33. mainWindow.Activate();
  34. mainWindow.Topmost = true;
  35. mainWindow.Topmost = false;
  36. mainWindow.Focus();
  37. }
  38. public void Exit()
  39. {
  40. IsExiting = true;
  41. Application.Current.MainWindow?.Close();
  42. Application.Current.Shutdown();
  43. }
  44. public void ShowBalloonTip()
  45. {
  46. _tray?.ShowBalloonTip("EEMS Client", "服务正在后台运行...", BalloonIcon.Info);
  47. }
  48. private void Dispose(bool disposing)
  49. {
  50. if (!disposedValue)
  51. {
  52. if (disposing)
  53. {
  54. // TODO: dispose managed state (managed objects)
  55. if (_tray != null)
  56. {
  57. _tray.Visibility = Visibility.Hidden;
  58. _tray.Dispose();
  59. _tray = null;
  60. }
  61. }
  62. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  63. // TODO: set large fields to null
  64. disposedValue = true;
  65. }
  66. }
  67. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  68. // ~TrayController()
  69. // {
  70. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  71. // Dispose(disposing: false);
  72. // }
  73. public void Dispose()
  74. {
  75. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  76. Dispose(disposing: true);
  77. GC.SuppressFinalize(this);
  78. }
  79. }