Guest User

Untitled

a guest
Jul 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using Microsoft.Phone.Controls;
  12.  
  13. namespace JeffWilcox.Controls
  14. {
  15. // Based upon Headered Content Control as it often comes up in these templates.
  16.  
  17. public class WindowBase : HeaderedContentControl
  18. {
  19. private PhoneApplicationPage _page;
  20.  
  21. private bool _transitionedIn;
  22.  
  23. private Panel _templateRoot;
  24.  
  25. public event EventHandler PressedBack;
  26.  
  27. private Grid _overlay;
  28.  
  29. public override void OnApplyTemplate()
  30. {
  31. base.OnApplyTemplate();
  32.  
  33. _templateRoot = MoreVisualTreeExtensions.FindFirstChildOfType<Panel>(this);
  34. if (_templateRoot == null)
  35. {
  36. throw new InvalidOperationException("Must include a Panel in the root of the template.");
  37. }
  38.  
  39. if (!_transitionedIn)
  40. {
  41. var @in = GetTransitionIn();
  42. if (@in != null)
  43. {
  44. var transition = @in.GetTransition(_templateRoot);
  45. transition.Completed += (x, xe) => transition.Stop();
  46. transition.Begin();
  47. }
  48. _transitionedIn = true;
  49. }
  50. }
  51.  
  52. private void BuildOverlay()
  53. {
  54. var bg = (Color)Resources["PhoneBackgroundColor"];
  55. _overlay = new Grid();
  56. _overlay.IsHitTestVisible = true;
  57. _overlay.Background = new SolidColorBrush(Color.FromArgb(0xa0, bg.R, bg.G, bg.B));
  58. }
  59.  
  60. protected void InsertIntoVisualTree(Panel parentPanel)
  61. {
  62. BuildOverlay();
  63. parentPanel.Children.Add(_overlay);
  64. parentPanel.Children.Add(this);
  65.  
  66. // WARNING: This version will not attach to page back button
  67. // key presses and could fail ingestion.
  68. }
  69.  
  70. protected void InsertIntoFrame()
  71. {
  72. IExposeRootFrame ie = Application.Current as IExposeRootFrame;
  73. if (ie != null)
  74. {
  75. WilcoxTransitionFrame wtf = ie.RootFrame;
  76. InsertIntoVisualTree(wtf.OverlayGrid);
  77.  
  78. _page = wtf.Content as PhoneApplicationPage;
  79. if (_page != null)
  80. {
  81. _page.BackKeyPress += OnBackKeyPress;
  82. }
  83. }
  84. else throw new InvalidOperationException("Root must be of WilcoxTransitionFrame type.");
  85. }
  86.  
  87. private void OnBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
  88. {
  89. e.Cancel = true;
  90.  
  91. var handler = PressedBack;
  92. if (handler != null)
  93. {
  94. handler(this, EventArgs.Empty);
  95. }
  96.  
  97. CloseWindow();
  98. }
  99.  
  100. protected void CloseWindow()
  101. {
  102. // Remove from the parent visual tree.
  103. var me = this;
  104.  
  105. if (_page != null)
  106. {
  107. _page.BackKeyPress -= OnBackKeyPress;
  108. _page = null;
  109. }
  110.  
  111. Action removeVisualFromParent = () =>
  112. {
  113. Dispatcher.BeginInvoke(() =>
  114. {
  115. Panel p = me.Parent as Panel;
  116. if (p != null)
  117. {
  118. p.Children.Remove(me);
  119. }
  120. });
  121. };
  122. Action removeOverlay = () =>
  123. {
  124. Grid overlay = _overlay;
  125. _overlay = null;
  126. if (overlay != null)
  127. {
  128. Panel p = overlay.Parent as Panel;
  129. if (p != null)
  130. {
  131. p.Children.Remove(overlay);
  132. }
  133. }
  134. };
  135. Action removeVisuals = () =>
  136. {
  137. removeOverlay();
  138. removeVisualFromParent();
  139. };
  140.  
  141. // Animate.
  142. var @out = GetTransitionOut();
  143. if (@out != null)
  144. {
  145. var transition = @out.GetTransition(_templateRoot);
  146. transition.Completed += (x, xe) =>
  147. {
  148. me.Opacity = 0;
  149.  
  150. transition.Stop();
  151.  
  152. removeVisuals();
  153. };
  154. transition.Begin();
  155. }
  156. else
  157. {
  158. removeVisuals();
  159. }
  160. }
  161.  
  162. protected virtual TransitionElement GetTransitionIn()
  163. {
  164. return new SwivelTransition
  165. {
  166. Mode = SwivelTransitionMode.BackwardIn,
  167. };
  168. }
  169.  
  170. protected virtual TransitionElement GetTransitionOut()
  171. {
  172. return new SwivelTransition
  173. {
  174. Mode = SwivelTransitionMode.BackwardOut,
  175. };
  176. }
  177. }
  178. }
Add Comment
Please, Sign In to add comment