Screen.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. namespace Caliburn.Micro.Core {
  2. using System;
  3. /// <summary>
  4. /// A base implementation of <see cref = "IScreen" />.
  5. /// </summary>
  6. public class Screen : ViewAware, IScreen, IChild {
  7. static readonly ILog Log = LogManager.GetLog(typeof (Screen));
  8. bool isActive;
  9. bool isInitialized;
  10. object parent;
  11. string displayName;
  12. /// <summary>
  13. /// Creates an instance of the screen.
  14. /// </summary>
  15. public Screen() {
  16. displayName = GetType().FullName;
  17. }
  18. /// <summary>
  19. /// Gets or Sets the Parent <see cref = "IConductor" />
  20. /// </summary>
  21. public virtual object Parent {
  22. get { return parent; }
  23. set {
  24. parent = value;
  25. NotifyOfPropertyChange("Parent");
  26. }
  27. }
  28. /// <summary>
  29. /// Gets or Sets the Display Name
  30. /// </summary>
  31. public virtual string DisplayName {
  32. get { return displayName; }
  33. set {
  34. displayName = value;
  35. NotifyOfPropertyChange("DisplayName");
  36. }
  37. }
  38. /// <summary>
  39. /// Indicates whether or not this instance is currently active.
  40. /// Virtualized in order to help with document oriented view models.
  41. /// </summary>
  42. public virtual bool IsActive {
  43. get { return isActive; }
  44. private set {
  45. isActive = value;
  46. NotifyOfPropertyChange("IsActive");
  47. }
  48. }
  49. /// <summary>
  50. /// Indicates whether or not this instance is currently initialized.
  51. /// Virtualized in order to help with document oriented view models.
  52. /// </summary>
  53. public virtual bool IsInitialized {
  54. get { return isInitialized; }
  55. private set {
  56. isInitialized = value;
  57. NotifyOfPropertyChange("IsInitialized");
  58. }
  59. }
  60. /// <summary>
  61. /// Raised after activation occurs.
  62. /// </summary>
  63. public virtual event EventHandler<ActivationEventArgs> Activated = delegate { };
  64. /// <summary>
  65. /// Raised before deactivation.
  66. /// </summary>
  67. public virtual event EventHandler<DeactivationEventArgs> AttemptingDeactivation = delegate { };
  68. /// <summary>
  69. /// Raised after deactivation.
  70. /// </summary>
  71. public virtual event EventHandler<DeactivationEventArgs> Deactivated = delegate { };
  72. void IActivate.Activate() {
  73. if (IsActive) {
  74. return;
  75. }
  76. var initialized = false;
  77. if (!IsInitialized) {
  78. IsInitialized = initialized = true;
  79. OnInitialize();
  80. }
  81. IsActive = true;
  82. Log.Info("Activating {0}.", this);
  83. OnActivate();
  84. var handler = Activated;
  85. if (handler != null) {
  86. handler(this, new ActivationEventArgs
  87. {
  88. WasInitialized = initialized
  89. });
  90. }
  91. }
  92. /// <summary>
  93. /// Called when initializing.
  94. /// </summary>
  95. protected virtual void OnInitialize() {}
  96. /// <summary>
  97. /// Called when activating.
  98. /// </summary>
  99. protected virtual void OnActivate() {}
  100. void IDeactivate.Deactivate(bool close) {
  101. if (IsActive || (IsInitialized && close)) {
  102. var attemptingDeactivationHandler = AttemptingDeactivation;
  103. if (attemptingDeactivationHandler != null) {
  104. attemptingDeactivationHandler(this, new DeactivationEventArgs
  105. {
  106. WasClosed = close
  107. });
  108. }
  109. IsActive = false;
  110. Log.Info("Deactivating {0}.", this);
  111. OnDeactivate(close);
  112. var deactivatedHandler = Deactivated;
  113. if (deactivatedHandler != null) {
  114. deactivatedHandler(this, new DeactivationEventArgs
  115. {
  116. WasClosed = close
  117. });
  118. }
  119. if (close) {
  120. Views.Clear();
  121. Log.Info("Closed {0}.", this);
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Called when deactivating.
  127. /// </summary>
  128. /// <param name = "close">Inidicates whether this instance will be closed.</param>
  129. protected virtual void OnDeactivate(bool close) {}
  130. /// <summary>
  131. /// Called to check whether or not this instance can close.
  132. /// </summary>
  133. /// <param name = "callback">The implementor calls this action with the result of the close check.</param>
  134. public virtual void CanClose(Action<bool> callback) {
  135. callback(true);
  136. }
  137. /// <summary>
  138. /// Tries to close this instance by asking its Parent to initiate shutdown or by asking its corresponding view to close.
  139. /// Also provides an opportunity to pass a dialog result to it's corresponding view.
  140. /// </summary>
  141. /// <param name="dialogResult">The dialog result.</param>
  142. public virtual void TryClose(bool? dialogResult = null) {
  143. PlatformProvider.Current.GetViewCloseAction(this, Views.Values, dialogResult).OnUIThread();
  144. }
  145. }
  146. }