2
0

TransitionFrame.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using Microsoft.Expression.Media.Effects;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Animation;
  7. namespace MediaBrowser.UI.Controls
  8. {
  9. /// <summary>
  10. /// Class TransitionFrame
  11. /// </summary>
  12. public class TransitionFrame : Frame
  13. {
  14. /// <summary>
  15. /// The _content presenter
  16. /// </summary>
  17. private ContentPresenter _contentPresenter = null;
  18. #region DP TransitionType
  19. /// <summary>
  20. /// Gets or sets the type of the transition.
  21. /// </summary>
  22. /// <value>The type of the transition.</value>
  23. public TransitionEffect TransitionType
  24. {
  25. get { return (TransitionEffect)GetValue(TransitionTypeProperty); }
  26. set { SetValue(TransitionTypeProperty, value); }
  27. }
  28. // Using a DependencyProperty as the backing store for TransitionType. This enables animation, styling, binding, etc...
  29. /// <summary>
  30. /// The transition type property
  31. /// </summary>
  32. public static readonly DependencyProperty TransitionTypeProperty =
  33. DependencyProperty.Register("TransitionType", typeof(TransitionEffect), typeof(TransitionFrame),
  34. new UIPropertyMetadata(new BlindsTransitionEffect()));
  35. #endregion DP TransitionType
  36. #region DP Transition Animation
  37. /// <summary>
  38. /// Gets or sets the transition animation.
  39. /// </summary>
  40. /// <value>The transition animation.</value>
  41. public DoubleAnimation TransitionAnimation
  42. {
  43. get { return (DoubleAnimation)GetValue(TransitionAnimationProperty); }
  44. set { SetValue(TransitionAnimationProperty, value); }
  45. }
  46. // Using a DependencyProperty as the backing store for TransitionAnimation. This enables animation, styling, binding, etc...
  47. /// <summary>
  48. /// The transition animation property
  49. /// </summary>
  50. public static readonly DependencyProperty TransitionAnimationProperty =
  51. DependencyProperty.Register("TransitionAnimation", typeof(DoubleAnimation), typeof(TransitionFrame), new UIPropertyMetadata(null));
  52. #endregion DP Transition Animation
  53. /// <summary>
  54. /// Called when the template generation for the visual tree is created.
  55. /// </summary>
  56. public override void OnApplyTemplate()
  57. {
  58. // get a reference to the frame's content presenter
  59. // this is the element we will fade in and out
  60. _contentPresenter = GetTemplateChild("PART_FrameCP") as ContentPresenter;
  61. base.OnApplyTemplate();
  62. }
  63. /// <summary>
  64. /// Animates the content.
  65. /// </summary>
  66. /// <param name="navigationAction">The navigation action.</param>
  67. /// <param name="checkContent">if set to <c>true</c> [check content].</param>
  68. /// <param name="isBack">if set to <c>true</c> [is back].</param>
  69. private void AnimateContent(Action navigationAction, bool checkContent = true, bool isBack = false)
  70. {
  71. if (TransitionType == null || (checkContent && Content == null))
  72. {
  73. CommandBindings.Clear();
  74. navigationAction();
  75. CommandBindings.Clear();
  76. return;
  77. }
  78. var oldContentVisual = this as FrameworkElement;
  79. _contentPresenter.IsHitTestVisible = false;
  80. var da = TransitionAnimation.Clone();
  81. da.From = 0;
  82. da.To = 1;
  83. da.FillBehavior = FillBehavior.HoldEnd;
  84. var transitionEffect = TransitionType.Clone() as TransitionEffect;
  85. if (isBack)
  86. {
  87. ReverseDirection(transitionEffect);
  88. }
  89. transitionEffect.OldImage = new VisualBrush(oldContentVisual);
  90. transitionEffect.BeginAnimation(TransitionEffect.ProgressProperty, da);
  91. _contentPresenter.Effect = transitionEffect;
  92. _contentPresenter.IsHitTestVisible = true;
  93. // Remove base class bindings to remote buttons
  94. CommandBindings.Clear();
  95. navigationAction();
  96. CommandBindings.Clear();
  97. }
  98. /// <summary>
  99. /// Navigates the with transition.
  100. /// </summary>
  101. /// <param name="page">The page.</param>
  102. public void NavigateWithTransition(Page page)
  103. {
  104. AnimateContent(() => Navigate(page));
  105. }
  106. /// <summary>
  107. /// Navigates the with transition.
  108. /// </summary>
  109. /// <param name="page">The page.</param>
  110. public void NavigateWithTransition(Uri page)
  111. {
  112. AnimateContent(() => Navigate(page));
  113. }
  114. /// <summary>
  115. /// Goes the back with transition.
  116. /// </summary>
  117. public void GoBackWithTransition()
  118. {
  119. if (CanGoBack)
  120. {
  121. AnimateContent(GoBack, false, true);
  122. }
  123. }
  124. /// <summary>
  125. /// Goes the forward with transition.
  126. /// </summary>
  127. public void GoForwardWithTransition()
  128. {
  129. if (CanGoForward)
  130. {
  131. AnimateContent(GoForward, false);
  132. }
  133. }
  134. /// <summary>
  135. /// Reverses the direction.
  136. /// </summary>
  137. /// <param name="transitionEffect">The transition effect.</param>
  138. private void ReverseDirection(TransitionEffect transitionEffect)
  139. {
  140. var circleRevealTransitionEffect = transitionEffect as CircleRevealTransitionEffect;
  141. if (circleRevealTransitionEffect != null)
  142. {
  143. circleRevealTransitionEffect.Reverse = true;
  144. return;
  145. }
  146. var slideInTransitionEffect = transitionEffect as SlideInTransitionEffect;
  147. if (slideInTransitionEffect != null)
  148. {
  149. if (slideInTransitionEffect.SlideDirection == SlideDirection.RightToLeft)
  150. {
  151. slideInTransitionEffect.SlideDirection = SlideDirection.LeftToRight;
  152. }
  153. return;
  154. }
  155. var wipeTransitionEffect = transitionEffect as WipeTransitionEffect;
  156. if (wipeTransitionEffect != null)
  157. {
  158. if (wipeTransitionEffect.WipeDirection == WipeDirection.RightToLeft)
  159. {
  160. wipeTransitionEffect.WipeDirection = WipeDirection.LeftToRight;
  161. }
  162. }
  163. }
  164. }
  165. }