ShellSwitcher.cs 823 B

1234567891011121314151617181920212223242526272829
  1. using System.Linq;
  2. using System.Windows;
  3. using System.Threading;
  4. using System.Reflection;
  5. using SHJX.Service.Common.ElementHelper;
  6. namespace PrismMetroSample.Shell.Common
  7. {
  8. public static class ShellSwitcher
  9. {
  10. public static void Switch<TClosed, TShow>() where TClosed : Window where TShow : Window, new()
  11. {
  12. Show<TShow>();
  13. Closed<TClosed>();
  14. }
  15. public static void Show<T>(T window = null) where T : Window, new()
  16. {
  17. var shell = Application.Current.MainWindow = window ?? new T();
  18. shell?.Show();
  19. }
  20. public static void Closed<T>() where T : Window
  21. {
  22. var shell = Application.Current.Windows.OfType<Window>().FirstOrDefault(window => window is T);
  23. shell?.Close();
  24. }
  25. }
  26. }