ModalActionWindow.xaml.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows;
  4. using System.Windows.Interop;
  5. using vCloud.GeneratePackages.Tool.ViewModels;
  6. using Vinno.IUS.Common.Log;
  7. namespace vCloud.GeneratePackages.Tool
  8. {
  9. public interface IDescriptionUpdater
  10. {
  11. /// <summary>
  12. /// Update Discription
  13. /// </summary>
  14. /// <param name="desc"></param>
  15. void Update(string desc, bool writeLogger = true);
  16. /// <summary>
  17. /// The event Update Discription
  18. /// </summary>
  19. event EventHandler<string> DescriptionUpdated;
  20. }
  21. public class DescriptionUpdater : IDescriptionUpdater
  22. {
  23. /// <summary>
  24. /// The event Update Discription
  25. /// </summary>
  26. public event EventHandler<string> DescriptionUpdated;
  27. /// <summary>
  28. /// Update Discription
  29. /// </summary>
  30. /// <param name="desc"></param>
  31. public void Update(string desc, bool writeLogger = true)
  32. {
  33. if (writeLogger)
  34. {
  35. Logger.WriteLineInfo(desc);
  36. }
  37. DescriptionUpdated.Invoke(this, desc);
  38. }
  39. }
  40. /// <summary>
  41. /// ModalActionWindow.xaml 的交互逻辑
  42. /// </summary>
  43. public partial class ModalActionWindow : Window
  44. {
  45. ModalActionViewModel _viewModel;
  46. private const int GWL_STYLE = -16;
  47. private const int WS_SYSMENU = 0x80000;
  48. [DllImport("user32.dll", SetLastError = true)]
  49. private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  50. [DllImport("user32.dll")]
  51. private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  52. public ModalActionWindow(Action action, IDescriptionUpdater descriptionUpdater, string descpription = null, string title = null)
  53. {
  54. InitializeComponent();
  55. Loaded += OnWindow_Loaded;
  56. _viewModel = new ModalActionViewModel(action, descriptionUpdater, descpription, title);
  57. _viewModel.ClosedRequest += OnClose;
  58. DataContext = _viewModel;
  59. }
  60. private void OnWindow_Loaded(object sender, RoutedEventArgs e)
  61. {
  62. var hwnd = new WindowInteropHelper(this).Handle;
  63. SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
  64. }
  65. /// <summary>
  66. /// Update description
  67. /// </summary>
  68. /// <param name="desc"></param>
  69. public void Update(string desc)
  70. {
  71. Dispatcher.Invoke(()=> {
  72. if (_viewModel != null)
  73. {
  74. _viewModel.Description = desc;
  75. }
  76. });
  77. }
  78. private void OnClose(object sender, EventArgs e)
  79. {
  80. Loaded -= OnWindow_Loaded;
  81. _viewModel.ClosedRequest -= OnClose;
  82. Close();
  83. }
  84. }
  85. }