TransitionControl.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Microsoft.Expression.Media.Effects;
  2. using System.ComponentModel;
  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. /// http://victorcher.blogspot.com/2012/02/wpf-transactions.html
  11. /// </summary>
  12. public class TransitionControl : ContentControl
  13. {
  14. /// <summary>
  15. /// The _content presenter
  16. /// </summary>
  17. private ContentPresenter _contentPresenter;
  18. /// <summary>
  19. /// Initializes static members of the <see cref="TransitionControl" /> class.
  20. /// </summary>
  21. static TransitionControl()
  22. {
  23. DefaultStyleKeyProperty.OverrideMetadata(
  24. typeof(TransitionControl), new FrameworkPropertyMetadata(typeof(TransitionControl)));
  25. ContentProperty.OverrideMetadata(
  26. typeof(TransitionControl), new FrameworkPropertyMetadata(OnContentPropertyChanged));
  27. }
  28. /// <summary>
  29. /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />.
  30. /// </summary>
  31. public override void OnApplyTemplate()
  32. {
  33. base.OnApplyTemplate();
  34. _contentPresenter = (ContentPresenter)Template.FindName("ContentPresenter", this);
  35. }
  36. #region DP TransitionType
  37. /// <summary>
  38. /// Gets or sets the type of the transition.
  39. /// </summary>
  40. /// <value>The type of the transition.</value>
  41. public TransitionEffect TransitionType
  42. {
  43. get { return (TransitionEffect)GetValue(TransitionTypeProperty); }
  44. set { SetValue(TransitionTypeProperty, value); }
  45. }
  46. // Using a DependencyProperty as the backing store for TransitionType. This enables animation, styling, binding, etc...
  47. /// <summary>
  48. /// The transition type property
  49. /// </summary>
  50. public static readonly DependencyProperty TransitionTypeProperty =
  51. DependencyProperty.Register("TransitionType", typeof(TransitionEffect), typeof(TransitionControl),
  52. new UIPropertyMetadata(new BlindsTransitionEffect()));
  53. #endregion DP TransitionType
  54. #region DP Transition Animation
  55. /// <summary>
  56. /// Gets or sets the transition animation.
  57. /// </summary>
  58. /// <value>The transition animation.</value>
  59. public DoubleAnimation TransitionAnimation
  60. {
  61. get { return (DoubleAnimation)GetValue(TransitionAnimationProperty); }
  62. set { SetValue(TransitionAnimationProperty, value); }
  63. }
  64. // Using a DependencyProperty as the backing store for TransitionAnimation. This enables animation, styling, binding, etc...
  65. /// <summary>
  66. /// The transition animation property
  67. /// </summary>
  68. public static readonly DependencyProperty TransitionAnimationProperty =
  69. DependencyProperty.Register("TransitionAnimation", typeof(DoubleAnimation), typeof(TransitionControl), new UIPropertyMetadata(null));
  70. #endregion DP Transition Animation
  71. /// <summary>
  72. /// Called when [content property changed].
  73. /// </summary>
  74. /// <param name="dp">The dp.</param>
  75. /// <param name="args">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
  76. private static void OnContentPropertyChanged(DependencyObject dp, DependencyPropertyChangedEventArgs args)
  77. {
  78. var oldContent = args.OldValue;
  79. var newContent = args.NewValue;
  80. var transitionControl = (TransitionControl)dp;
  81. if (DesignerProperties.GetIsInDesignMode(transitionControl))
  82. return;
  83. if (oldContent != null && newContent != null && transitionControl.IsVisible)
  84. {
  85. transitionControl.AnimateContent(oldContent, newContent);
  86. }
  87. else if (newContent != null)
  88. {
  89. transitionControl.Content = newContent;
  90. }
  91. }
  92. /// <summary>
  93. /// Animates the content.
  94. /// </summary>
  95. /// <param name="oldContent">The old content.</param>
  96. /// <param name="newContent">The new content.</param>
  97. private void AnimateContent(object oldContent, object newContent)
  98. {
  99. FrameworkElement oldContentVisual;
  100. try
  101. {
  102. oldContentVisual = VisualTreeHelper.GetChild(_contentPresenter, 0) as FrameworkElement;
  103. }
  104. catch
  105. {
  106. return;
  107. }
  108. var transitionEffect = TransitionType;
  109. if (transitionEffect == null)
  110. {
  111. _contentPresenter.Content = newContent;
  112. return;
  113. }
  114. var da = TransitionAnimation;
  115. da.From = 0;
  116. da.To = 1;
  117. da.FillBehavior = FillBehavior.HoldEnd;
  118. transitionEffect.OldImage = new VisualBrush(oldContentVisual);
  119. transitionEffect.BeginAnimation(TransitionEffect.ProgressProperty, da);
  120. _contentPresenter.Effect = transitionEffect;
  121. _contentPresenter.Content = newContent;
  122. }
  123. }
  124. }