NChildWindow.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Markup;
  12. using System.Windows.Threading;
  13. using FWindSoft.Data;
  14. namespace FWindSoft.Wpf.Controls
  15. {
  16. /// <summary>
  17. /// 导航窗体
  18. /// </summary>
  19. [ContentProperty("Content")]
  20. public class NChildWindow:UserControl
  21. {
  22. public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title",
  23. typeof(string),
  24. typeof(NChildWindow), new PropertyMetadata(string.Empty));
  25. public static readonly DependencyProperty AttachElementProperty = DependencyProperty.Register("AttachElement",
  26. typeof(UIElement),
  27. typeof(NChildWindow), new PropertyMetadata(null));
  28. public NChildWindow()
  29. {
  30. BindingWidth();
  31. this.Loaded+=NChildWindow_Loaded;
  32. this.Unloaded+=NChildWindow_Unloaded;
  33. //AttachElements = new ObservableCollection<UIElement>();
  34. }
  35. #region 事件封装
  36. private static void NChildWindow_Unloaded(object sender, RoutedEventArgs e)
  37. {
  38. NChildWindow nW = sender as NChildWindow;
  39. if (nW == null)
  40. return;
  41. nW.OnClose(new NChildWindowArgs(e) { RefParameter = NavigationBar.GetParameter(nW) });
  42. }
  43. private static void NChildWindow_Loaded(object sender, RoutedEventArgs e)
  44. {
  45. NChildWindow nW = sender as NChildWindow;
  46. if (nW == null)
  47. return;
  48. nW.OnLoad(new NChildWindowArgs(e) { RefParameter = NavigationBar.GetParameter(nW) });
  49. }
  50. #endregion
  51. protected virtual void OnLoad(NChildWindowArgs args )
  52. {
  53. if (EveryTimeLoad || !m_IsLoadData)
  54. {
  55. if (OnceBreakLoad.Value)
  56. {
  57. return;
  58. }
  59. this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => {
  60. if (args.RefParameter != null)
  61. {
  62. LoadData(args.RefParameter.StartParameter);
  63. }
  64. else
  65. {
  66. LoadData(null);
  67. }
  68. m_IsLoadData = true;
  69. }));
  70. }
  71. }
  72. protected virtual void OnClose(NChildWindowArgs args)
  73. {
  74. }
  75. /// <summary>
  76. /// 窗口名称
  77. /// </summary>
  78. public string Title
  79. {
  80. get { return this.GetValue(TitleProperty) as string; }
  81. set { this.SetValue(TitleProperty,value); }
  82. }
  83. #region 打开窗体
  84. public virtual void Show()
  85. {
  86. if (NavigationBar.CurrentNavigation != null)
  87. {
  88. NavigationBar.CurrentNavigation.Add(this);
  89. }
  90. }
  91. public virtual void Show(NavigationBar bar)
  92. {
  93. if (bar != null)
  94. {
  95. bar.Add(this);
  96. }
  97. }
  98. public virtual void Show(UIElement parent)
  99. {
  100. var bar = parent.GetParentType<NavigationBar>();
  101. if (bar != null)
  102. {
  103. bar.Add(this);
  104. }
  105. }
  106. public void Show(NParameter parameter)
  107. {
  108. NavigationBar.SetParameter(this, parameter);
  109. Show();
  110. }
  111. #endregion
  112. #region 加载信息模块
  113. private bool m_IsLoadData = false;
  114. public void ResetLoadData()
  115. {
  116. this.m_IsLoadData = false;
  117. }
  118. /// <summary>
  119. /// 每次都加载
  120. /// </summary>
  121. public bool EveryTimeLoad { get; protected set; }
  122. /// <summary>
  123. /// 是否打断一次加载【首次加载也可打断】
  124. /// </summary>
  125. public MomentVariable<bool> OnceBreakLoad { get; private set; } = new MomentVariable<bool>(false);
  126. protected virtual void LoadData(LoadParameter parameter)
  127. {
  128. }
  129. #endregion
  130. public void SetStartParameter(NParameter parameter)
  131. {
  132. NavigationBar.SetParameter(this, parameter);
  133. }
  134. public virtual void Close()
  135. {
  136. if (NavigationBar.CurrentNavigation != null)
  137. {
  138. NavigationBar.CurrentNavigation.Remove(this);
  139. }
  140. }
  141. public void Close(NParameter parameter)
  142. {
  143. NavigationBar.SetParameter(this, parameter);
  144. Close();
  145. }
  146. /// <summary>
  147. /// 附加元素
  148. /// </summary>
  149. public UIElement AttachElement
  150. {
  151. get { return this.GetValue(AttachElementProperty) as UIElement; }
  152. set { this.SetValue(AttachElementProperty, value); }
  153. }
  154. #region 附加功能
  155. private void BindingWidth()
  156. {
  157. RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
  158. rs.AncestorType = typeof(ScrollContentPresenter);
  159. Binding binding = new Binding("ActualWidth") { RelativeSource = rs };
  160. this.SetBinding(WidthProperty, binding);
  161. }
  162. #endregion
  163. }
  164. public class NChildWindowArgs : RoutedEventArgs
  165. {
  166. public NChildWindowArgs(RoutedEventArgs args)
  167. {
  168. this.RoutedEvent = args.RoutedEvent;//他的顺序要在handled赋值之上
  169. this.Source = args.Source;
  170. this.Handled = args.Handled;
  171. }
  172. /// <summary>
  173. /// 关联参数
  174. /// </summary>
  175. public NParameter RefParameter { get; set; }
  176. }
  177. }