ContentManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace aipdev
  7. {
  8. static class ContentManager
  9. {
  10. private static ContentControl _contentControl;
  11. private static readonly Dictionary<string, IContentPage> PageInstances = new Dictionary<string, IContentPage>();
  12. private static readonly List<IContentPage> VisiblePages = new List<IContentPage>();
  13. private static WaitingSpinner _waitingSpinner;
  14. /// <summary>
  15. /// Gets if the back operation can be executed.
  16. /// </summary>
  17. public static bool CanBack { get; private set; }
  18. /// <summary>
  19. /// Raised when the visible page changed.
  20. /// </summary>
  21. public static event EventHandler VisiblePageChanged;
  22. /// <summary>
  23. /// Gets the current visible page.
  24. /// </summary>
  25. public static IContentPage CurrentVisiblePage => VisiblePages.LastOrDefault();
  26. /// <summary>
  27. /// Register main content control.
  28. /// </summary>
  29. /// <param name="contentControl"></param>
  30. public static void RegisterContentControl(ContentControl contentControl)
  31. {
  32. _contentControl = contentControl;
  33. }
  34. /// <summary>
  35. /// Register a loading icon into the system.
  36. /// </summary>
  37. /// <param name="waitingSpinner"></param>
  38. public static void RegisterLoadingControl(WaitingSpinner waitingSpinner)
  39. {
  40. _waitingSpinner = waitingSpinner;
  41. }
  42. /// <summary>
  43. /// Show loading icon and prevent operations.
  44. /// </summary>
  45. public static void ShowLoading()
  46. {
  47. if (_waitingSpinner?.Visibility == Visibility.Collapsed)
  48. {
  49. _waitingSpinner?.StartWaiting();
  50. }
  51. }
  52. /// <summary>
  53. /// Hide loading icon.
  54. /// </summary>
  55. public static void HideLoading()
  56. {
  57. if (_waitingSpinner?.Visibility == Visibility.Visible)
  58. {
  59. _waitingSpinner?.StopWaiting();
  60. }
  61. }
  62. /// <summary>
  63. /// Register a content page instance.
  64. /// </summary>
  65. /// <param name="page"></param>
  66. public static void RegisterContentPage(IContentPage page)
  67. {
  68. if (!PageInstances.ContainsKey(page.PageName))
  69. {
  70. PageInstances.Add(page.PageName, page);
  71. }
  72. }
  73. /// <summary>
  74. /// Show new page.
  75. /// </summary>
  76. /// <param name="pageName">The name of page to show</param>
  77. /// <param name="arg">The arg which pass to the page.</param>
  78. public static void Show(string pageName, object arg = null)
  79. {
  80. if (_contentControl != null)
  81. {
  82. var currentPage = CurrentVisiblePage;
  83. if (currentPage != null && currentPage.IsModalPage)
  84. {
  85. return;
  86. }
  87. if (PageInstances.ContainsKey(pageName))
  88. {
  89. var page = PageInstances[pageName];
  90. if (page.IsHomePage)
  91. {
  92. VisiblePages.Clear();
  93. VisiblePages.Add(page);
  94. _contentControl.Content = page;
  95. page.OnShow(arg);
  96. }
  97. else
  98. {
  99. var visiblePage = VisiblePages.FirstOrDefault(x => x.PageName == pageName);
  100. if (visiblePage == null)
  101. {
  102. VisiblePages.Add(page);
  103. _contentControl.Content = page;
  104. page.OnShow(arg);
  105. }
  106. else
  107. {
  108. VisiblePages.Remove(visiblePage);
  109. VisiblePages.Add(visiblePage);
  110. _contentControl.Content = visiblePage;
  111. visiblePage.OnShow(arg);
  112. }
  113. }
  114. }
  115. }
  116. if (VisiblePages.Count > 1)
  117. {
  118. CanBack = !CurrentVisiblePage.IsModalPage;
  119. }
  120. else
  121. {
  122. CanBack = false;
  123. }
  124. VisiblePageChanged?.Invoke(null, EventArgs.Empty);
  125. }
  126. /// <summary>
  127. /// Back to last page.
  128. /// </summary>
  129. public static void Back()
  130. {
  131. if (_contentControl != null)
  132. {
  133. var currentPage = CurrentVisiblePage;
  134. if (currentPage != null && currentPage.IsModalPage)
  135. {
  136. return;
  137. }
  138. if (VisiblePages.Count > 1)
  139. {
  140. var lastPage = VisiblePages[^1];
  141. lastPage.OnHide();
  142. VisiblePages.Remove(lastPage);
  143. lastPage = VisiblePages[^1];
  144. _contentControl.Content = lastPage;
  145. }
  146. }
  147. if (VisiblePages.Count > 1)
  148. {
  149. CanBack = !CurrentVisiblePage.IsModalPage;
  150. }
  151. else
  152. {
  153. CanBack = false;
  154. }
  155. VisiblePageChanged?.Invoke(null, EventArgs.Empty);
  156. }
  157. /// <summary>
  158. /// Force make the page to invisible.
  159. /// </summary>
  160. /// <param name="contentPage">The page to invisible.</param>
  161. public static void Hide(this IContentPage contentPage)
  162. {
  163. if (_contentControl != null)
  164. {
  165. if (VisiblePages.Count > 1)
  166. {
  167. VisiblePages.RemoveAt(VisiblePages.Count -1);
  168. var lastPage = VisiblePages[^1];
  169. _contentControl.Content = lastPage;
  170. }
  171. }
  172. if (VisiblePages.Count > 1)
  173. {
  174. CanBack = !CurrentVisiblePage.IsModalPage;
  175. }
  176. else
  177. {
  178. CanBack = false;
  179. }
  180. VisiblePageChanged?.Invoke(null, EventArgs.Empty);
  181. }
  182. }
  183. }