UnloadCleanWindow.cs 1016 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Windows.Controls;
  3. using GalaSoft.MvvmLight;
  4. using GalaSoft.MvvmLight.Messaging;
  5. using Muchinfo.MTPClient.Infrastructure.Helpers;
  6. namespace Muchinfo.MTPClient.Infrastructure.Windows
  7. {
  8. public class UnloadCleanWindow : UserControl, IDisposable
  9. {
  10. public UnloadCleanWindow()
  11. {
  12. this.Unloaded += AutoCleanWindow_Unloaded;
  13. }
  14. protected void AutoCleanWindow_Unloaded(object sender, System.Windows.RoutedEventArgs e)
  15. {
  16. MessengerHelper.DefaultUnregister(this);
  17. var viewModel = this.DataContext as ViewModelBase;
  18. if (viewModel != null)
  19. {
  20. viewModel.Cleanup();
  21. }
  22. }
  23. /// <summary>
  24. /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
  25. /// </summary>
  26. public virtual void Dispose()
  27. {
  28. AutoCleanWindow_Unloaded(null, null);
  29. this.DataContext = null;
  30. }
  31. }
  32. }