SingleInstance.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:SingleInstance
  3. * 作者:xulisong
  4. * 创建时间: 2018/12/21 9:01:34
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using FWindSoft.Wpf;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. namespace Com.FirmLib.UI
  16. {
  17. public class WindowSingleInstance<T> where T:Window
  18. {
  19. private readonly Func<T> m_CreateInstance;
  20. public WindowSingleInstance()
  21. {
  22. m_CreateInstance = CreateInstance;
  23. }
  24. public WindowSingleInstance(Func<T> createInstance)
  25. {
  26. m_CreateInstance = createInstance;
  27. }
  28. protected T CreateInstance()
  29. {
  30. Type type = typeof(T);
  31. ConstructorInfo construct = type.GetConstructor(Type.EmptyTypes);
  32. if (construct == null)
  33. return default(T);
  34. return (T)construct.Invoke(null);
  35. }
  36. protected T m_Target;
  37. /// <summary>
  38. /// 目标信息
  39. /// </summary>
  40. public virtual T Target
  41. {
  42. get
  43. {
  44. if (m_Target == null||m_Target.IsDisposed())
  45. {
  46. if (m_CreateInstance!=null)
  47. {
  48. m_Target = m_CreateInstance();
  49. }
  50. }
  51. else
  52. {
  53. m_Target.Visibility = Visibility.Visible;
  54. m_Target.Activate();
  55. }
  56. return m_Target;
  57. }
  58. }
  59. }
  60. }