You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

761 lines
28 KiB

  1. using BPA.UIControl.Commons.KnownBoxes;
  2. using System;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using System.Windows.Media.Animation;
  8. namespace BPA.UIControl
  9. {
  10. /// <summary>
  11. /// 转换动画
  12. /// </summary>
  13. public class Transition : ContentControl
  14. {
  15. static Transition()
  16. {
  17. DefaultStyleKeyProperty.OverrideMetadata(typeof(Transition), new FrameworkPropertyMetadata(typeof(Transition)));
  18. }
  19. /// <inheritdoc/>
  20. public override void OnApplyTemplate()
  21. {
  22. base.OnApplyTemplate();
  23. Loaded += Transition_Loaded;
  24. }
  25. private void Transition_Loaded(object sender, RoutedEventArgs e)
  26. {
  27. if (IsShow && Type != TransitionType.None)
  28. {
  29. ShowAnimation(this);
  30. if (!PlayEveryTime)
  31. {
  32. Loaded -= Transition_Loaded;
  33. }
  34. }
  35. else if (Type >= TransitionType.CollapseUp && Type <= TransitionType.CollapseRight && CollapsedSize > 0)
  36. {
  37. if (Content is FrameworkElement element)
  38. {
  39. Progress = CollapsedSize / element.ActualWidth;
  40. }
  41. }
  42. Loaded -= Transition_Loaded;
  43. }
  44. #region 命令
  45. /// <summary>
  46. /// 显示完命令
  47. /// </summary>
  48. public static readonly DependencyProperty ShowedCommandProperty =
  49. DependencyProperty.Register("ShowedCommand", typeof(ICommand), typeof(Transition), new PropertyMetadata(default(ICommand)));
  50. /// <summary>
  51. /// 显示完命令
  52. /// </summary>
  53. public ICommand ShowedCommand
  54. {
  55. get { return (ICommand)GetValue(ShowedCommandProperty); }
  56. set { SetValue(ShowedCommandProperty, value); }
  57. }
  58. /// <summary>
  59. /// 关闭完命令
  60. /// </summary>
  61. public static readonly DependencyProperty ClosedCommandProperty =
  62. DependencyProperty.Register("ClosedCommand", typeof(ICommand), typeof(Transition), new PropertyMetadata(default(ICommand)));
  63. /// <summary>
  64. /// 关闭完命令
  65. /// </summary>
  66. public ICommand ClosedCommand
  67. {
  68. get { return (ICommand)GetValue(ClosedCommandProperty); }
  69. set { SetValue(ClosedCommandProperty, value); }
  70. }
  71. #endregion 命令
  72. #region 事件
  73. /// <summary>
  74. /// 显示完事件
  75. /// </summary>
  76. public static readonly RoutedEvent ShowedEvent =
  77. EventManager.RegisterRoutedEvent("Showed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Transition));
  78. /// <summary>
  79. /// 显示完事件 handler
  80. /// </summary>
  81. public event RoutedEventHandler Showed
  82. {
  83. add { AddHandler(ShowedEvent, value); }
  84. remove { RemoveHandler(ShowedEvent, value); }
  85. }
  86. /// <summary>
  87. /// 关闭完事件
  88. /// </summary>
  89. public static readonly RoutedEvent ClosedEvent =
  90. EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Transition));
  91. /// <summary>
  92. /// 关闭完事件 handler
  93. /// </summary>
  94. public event RoutedEventHandler Closed
  95. {
  96. add { AddHandler(ClosedEvent, value); }
  97. remove { RemoveHandler(ClosedEvent, value); }
  98. }
  99. #endregion 事件
  100. #region 依赖属性
  101. /// <summary>
  102. /// 是否显示
  103. /// </summary>
  104. public static readonly DependencyProperty IsShowProperty =
  105. DependencyProperty.Register("IsShow", typeof(bool), typeof(Transition), new PropertyMetadata(BooleanBoxes.FalseBox, OnIsShowChanged));
  106. /// <summary>
  107. /// 是否显示
  108. /// </summary>
  109. public bool IsShow
  110. {
  111. get { return (bool)GetValue(IsShowProperty); }
  112. set { SetValue(IsShowProperty, BooleanBoxes.Box(value)); }
  113. }
  114. /// <summary>
  115. /// 每次加载显示动画
  116. /// </summary>
  117. public static readonly DependencyProperty PlayEveryTimeProperty =
  118. DependencyProperty.Register("PlayEveryTime", typeof(bool), typeof(Transition), new PropertyMetadata(BooleanBoxes.FalseBox));
  119. /// <summary>
  120. /// 每次加载显示动画
  121. /// </summary>
  122. public bool PlayEveryTime
  123. {
  124. get { return (bool)GetValue(PlayEveryTimeProperty); }
  125. set { SetValue(PlayEveryTimeProperty, BooleanBoxes.Box(value)); }
  126. }
  127. /// <summary>
  128. /// 转换类型
  129. /// </summary>
  130. public static readonly DependencyProperty TypeProperty =
  131. DependencyProperty.Register("Type", typeof(TransitionType), typeof(Transition), new PropertyMetadata(default(TransitionType)));
  132. /// <summary>
  133. /// 转换类型
  134. /// </summary>
  135. public TransitionType Type
  136. {
  137. get { return (TransitionType)GetValue(TypeProperty); }
  138. set { SetValue(TypeProperty, value); }
  139. }
  140. /// <summary>
  141. /// 动画时长
  142. /// </summary>
  143. public static readonly DependencyProperty DurationProperty =
  144. DependencyProperty.Register("Duration", typeof(Duration), typeof(Transition), new PropertyMetadata(default(Duration)));
  145. /// <summary>
  146. /// 动画时长
  147. /// </summary>
  148. public Duration Duration
  149. {
  150. get { return (Duration)GetValue(DurationProperty); }
  151. set { SetValue(DurationProperty, value); }
  152. }
  153. /// <summary>
  154. /// 动画位置偏移
  155. /// </summary>
  156. public static readonly DependencyProperty OffsetProperty =
  157. DependencyProperty.Register("Offset", typeof(double), typeof(Transition), new PropertyMetadata(default(double)));
  158. /// <summary>
  159. /// 动画位置偏移
  160. /// </summary>
  161. public double Offset
  162. {
  163. get { return (double)GetValue(OffsetProperty); }
  164. set { SetValue(OffsetProperty, value); }
  165. }
  166. /// <summary>
  167. /// 动画初始缩放
  168. /// </summary>
  169. public static readonly DependencyProperty InitialScaleProperty =
  170. DependencyProperty.Register("InitialScale", typeof(double), typeof(Transition), new PropertyMetadata(default(double)));
  171. /// <summary>
  172. /// 动画初始缩放
  173. /// </summary>
  174. public double InitialScale
  175. {
  176. get { return (double)GetValue(InitialScaleProperty); }
  177. set { SetValue(InitialScaleProperty, value); }
  178. }
  179. /// <summary>
  180. /// 显示缓动
  181. /// </summary>
  182. public static readonly DependencyProperty ShowEasingFunctionProperty =
  183. DependencyProperty.Register("ShowEasingFunction", typeof(IEasingFunction), typeof(Transition), new PropertyMetadata(default(IEasingFunction)));
  184. /// <summary>
  185. /// 显示缓动
  186. /// </summary>
  187. public IEasingFunction ShowEasingFunction
  188. {
  189. get { return (IEasingFunction)GetValue(ShowEasingFunctionProperty); }
  190. set { SetValue(ShowEasingFunctionProperty, value); }
  191. }
  192. /// <summary>
  193. /// 关闭缓动
  194. /// </summary>
  195. public static readonly DependencyProperty CloseEasingFunctionProperty =
  196. DependencyProperty.Register("CloseEasingFunction", typeof(IEasingFunction), typeof(Transition), new PropertyMetadata(default(IEasingFunction)));
  197. /// <summary>
  198. /// 关闭缓动
  199. /// </summary>
  200. public IEasingFunction CloseEasingFunction
  201. {
  202. get { return (IEasingFunction)GetValue(CloseEasingFunctionProperty); }
  203. set { SetValue(CloseEasingFunctionProperty, value); }
  204. }
  205. /// <summary>
  206. /// 动画进度
  207. /// 显示完成 = 1
  208. /// 隐藏完成 = 0
  209. /// </summary>
  210. public static readonly DependencyProperty ProgressProperty =
  211. DependencyProperty.Register("Progress", typeof(double), typeof(Transition), new PropertyMetadata(default(double)));
  212. /// <summary>
  213. /// 动画进度
  214. /// 显示完成 = 1
  215. /// 隐藏完成 = 0
  216. /// </summary>
  217. public double Progress
  218. {
  219. get { return (double)GetValue(ProgressProperty); }
  220. set { SetValue(ProgressProperty, value); }
  221. }
  222. /// <summary>
  223. /// 是否淡入淡出
  224. /// </summary>
  225. public static readonly DependencyProperty IsFadeProperty =
  226. DependencyProperty.Register("IsFade", typeof(bool), typeof(Transition), new PropertyMetadata(BooleanBoxes.FalseBox));
  227. /// <summary>
  228. /// 是否淡入淡出
  229. /// </summary>
  230. public bool IsFade
  231. {
  232. get { return (bool)GetValue(IsFadeProperty); }
  233. set { SetValue(IsFadeProperty, BooleanBoxes.Box(value)); }
  234. }
  235. /// <summary>
  236. /// 折叠后的大小
  237. /// </summary>
  238. public static readonly DependencyProperty CollapsedSizeProperty =
  239. DependencyProperty.Register("CollapsedSize", typeof(double), typeof(Transition), new PropertyMetadata(default(double), OnIsShowChanged));
  240. /// <summary>
  241. /// 折叠后的大小
  242. /// </summary>
  243. public double CollapsedSize
  244. {
  245. get { return (double)GetValue(CollapsedSizeProperty); }
  246. set { SetValue(CollapsedSizeProperty, value); }
  247. }
  248. #endregion 依赖属性
  249. /// <summary>
  250. /// 关闭转换器
  251. /// </summary>
  252. /// <returns>关闭是否完成</returns>
  253. public async Task<bool> Close()
  254. {
  255. var taskCompletionSource = new TaskCompletionSource<bool>();
  256. this.Closed += (a, b) => taskCompletionSource.TrySetResult(true);
  257. return await taskCompletionSource.Task;
  258. }
  259. private static void OnIsShowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  260. {
  261. var transition = d as Transition;
  262. ChangeTransitionVisual(transition);
  263. }
  264. private static void ChangeTransitionVisual(Transition transition)
  265. {
  266. if (transition.IsLoaded && transition.Type != TransitionType.None)
  267. {
  268. if (transition.IsShow)
  269. {
  270. ShowAnimation(transition);
  271. }
  272. else
  273. {
  274. CloseAnimation(transition);
  275. }
  276. }
  277. else if (transition.IsInitialized || transition.Type == TransitionType.None)
  278. {
  279. transition.BeginAnimation(Transition.ProgressProperty, null);
  280. if (transition.IsShow)
  281. {
  282. transition.Progress = 1;
  283. }
  284. else
  285. {
  286. transition.Progress = 0;
  287. }
  288. }
  289. }
  290. /// <summary>
  291. /// 开始显示动画
  292. /// </summary>
  293. /// <param name="transition">转换</param>
  294. public static void ShowAnimation(Transition transition)
  295. {
  296. if (transition.Type == TransitionType.None)
  297. {
  298. return;
  299. }
  300. Storyboard storyboard = new Storyboard();
  301. storyboard.Completed += (sender, e) =>
  302. {
  303. var args = new RoutedEventArgs();
  304. args.RoutedEvent = Transition.ShowedEvent;
  305. transition.RaiseEvent(args);
  306. transition.ShowedCommand?.Execute(null);
  307. };
  308. DoubleAnimation progressAnimation;
  309. if (transition.Type >= TransitionType.CollapseUp && transition.Type <= TransitionType.CollapseRight)
  310. {
  311. progressAnimation = GetProgressAnimation(transition, 1, transition.ShowEasingFunction);
  312. }
  313. else
  314. {
  315. progressAnimation = GetProgressAnimation(transition, 1, transition.ShowEasingFunction, from: 0);
  316. }
  317. switch (transition.Type)
  318. {
  319. case TransitionType.Fade:
  320. case TransitionType.CollapseUp:
  321. case TransitionType.CollapseDown:
  322. case TransitionType.CollapseLeft:
  323. case TransitionType.CollapseRight:
  324. default:
  325. storyboard.Children.Add(progressAnimation);
  326. break;
  327. case TransitionType.FadeLeft:
  328. DoubleAnimation translateAnimation = GetTranslateXAnimation(transition, transition.Offset, 0, transition.ShowEasingFunction);
  329. storyboard.Children.Add(translateAnimation);
  330. storyboard.Children.Add(progressAnimation);
  331. break;
  332. case TransitionType.FadeRight:
  333. translateAnimation = GetTranslateXAnimation(transition, -transition.Offset, 0, transition.ShowEasingFunction);
  334. storyboard.Children.Add(translateAnimation);
  335. storyboard.Children.Add(progressAnimation);
  336. break;
  337. case TransitionType.FadeUp:
  338. translateAnimation = GetTranslateYAnimation(transition, transition.Offset, 0, transition.ShowEasingFunction);
  339. storyboard.Children.Add(translateAnimation);
  340. storyboard.Children.Add(progressAnimation);
  341. break;
  342. case TransitionType.FadeDown:
  343. translateAnimation = GetTranslateYAnimation(transition, -transition.Offset, 0, transition.ShowEasingFunction);
  344. storyboard.Children.Add(translateAnimation);
  345. storyboard.Children.Add(progressAnimation);
  346. break;
  347. case TransitionType.Zoom:
  348. DoubleAnimation scaleXAnimation = GetScaleXAnimation(transition, transition.InitialScale, 1, transition.ShowEasingFunction);
  349. DoubleAnimation scaleYAnimation = GetScaleYAnimation(transition, transition.InitialScale, 1, transition.ShowEasingFunction);
  350. storyboard.Children.Add(scaleXAnimation);
  351. storyboard.Children.Add(scaleYAnimation);
  352. storyboard.Children.Add(progressAnimation);
  353. break;
  354. case TransitionType.ZoomX:
  355. scaleXAnimation = GetScaleXAnimation(transition, transition.InitialScale, 1, transition.ShowEasingFunction);
  356. storyboard.Children.Add(scaleXAnimation);
  357. storyboard.Children.Add(progressAnimation);
  358. break;
  359. case TransitionType.ZoomY:
  360. scaleYAnimation = GetScaleYAnimation(transition, transition.InitialScale, 1, transition.ShowEasingFunction);
  361. storyboard.Children.Add(scaleYAnimation);
  362. storyboard.Children.Add(progressAnimation);
  363. break;
  364. case TransitionType.ZoomLeft:
  365. transition.RenderTransformOrigin = new Point(0, 0.5);
  366. scaleXAnimation = GetScaleXAnimation(transition, transition.InitialScale, 1, transition.ShowEasingFunction);
  367. storyboard.Children.Add(scaleXAnimation);
  368. storyboard.Children.Add(progressAnimation);
  369. break;
  370. case TransitionType.ZoomRight:
  371. transition.RenderTransformOrigin = new Point(1, 0.5);
  372. scaleXAnimation = GetScaleXAnimation(transition, transition.InitialScale, 1, transition.ShowEasingFunction);
  373. storyboard.Children.Add(scaleXAnimation);
  374. storyboard.Children.Add(progressAnimation);
  375. break;
  376. case TransitionType.ZoomUp:
  377. transition.RenderTransformOrigin = new Point(0.5, 0);
  378. scaleYAnimation = GetScaleYAnimation(transition, transition.InitialScale, 1, transition.ShowEasingFunction);
  379. storyboard.Children.Add(scaleYAnimation);
  380. storyboard.Children.Add(progressAnimation);
  381. break;
  382. case TransitionType.ZoomDown:
  383. transition.RenderTransformOrigin = new Point(0.5, 1);
  384. scaleYAnimation = GetScaleYAnimation(transition, transition.InitialScale, 1, transition.ShowEasingFunction);
  385. storyboard.Children.Add(scaleYAnimation);
  386. storyboard.Children.Add(progressAnimation);
  387. break;
  388. }
  389. storyboard.Begin();
  390. }
  391. /// <summary>
  392. /// 开始关闭动画
  393. /// </summary>
  394. /// <param name="transition">转换</param>
  395. public static void CloseAnimation(Transition transition)
  396. {
  397. if (transition.Type == TransitionType.None)
  398. {
  399. return;
  400. }
  401. Storyboard storyboard = new Storyboard();
  402. storyboard.Completed += (sender, e) =>
  403. {
  404. var args = new RoutedEventArgs();
  405. args.RoutedEvent = Transition.ClosedEvent;
  406. transition.RaiseEvent(args);
  407. transition.ClosedCommand?.Execute(null);
  408. };
  409. DoubleAnimation progressAnimation;
  410. if (transition.Type >= TransitionType.CollapseUp && transition.Type <= TransitionType.CollapseRight)
  411. {
  412. var progress = GetCollapsedProgress(transition);
  413. progressAnimation = GetProgressAnimation(transition, progress, transition.CloseEasingFunction);
  414. }
  415. else
  416. {
  417. progressAnimation = GetProgressAnimation(transition, 0, transition.CloseEasingFunction, from: 1);
  418. }
  419. switch (transition.Type)
  420. {
  421. case TransitionType.Fade:
  422. case TransitionType.CollapseUp:
  423. case TransitionType.CollapseDown:
  424. case TransitionType.CollapseLeft:
  425. case TransitionType.CollapseRight:
  426. default:
  427. storyboard.Children.Add(progressAnimation);
  428. break;
  429. case TransitionType.FadeLeft:
  430. DoubleAnimation translateAnimation = GetTranslateXAnimation(transition, 0, transition.Offset, transition.CloseEasingFunction);
  431. storyboard.Children.Add(translateAnimation);
  432. storyboard.Children.Add(progressAnimation);
  433. break;
  434. case TransitionType.FadeRight:
  435. translateAnimation = GetTranslateXAnimation(transition, 0, -transition.Offset, transition.CloseEasingFunction);
  436. storyboard.Children.Add(translateAnimation);
  437. storyboard.Children.Add(progressAnimation);
  438. break;
  439. case TransitionType.FadeUp:
  440. translateAnimation = GetTranslateYAnimation(transition, 0, transition.Offset, transition.CloseEasingFunction);
  441. storyboard.Children.Add(translateAnimation);
  442. storyboard.Children.Add(progressAnimation);
  443. break;
  444. case TransitionType.FadeDown:
  445. translateAnimation = GetTranslateYAnimation(transition, 0, -transition.Offset, transition.CloseEasingFunction);
  446. storyboard.Children.Add(translateAnimation);
  447. storyboard.Children.Add(progressAnimation);
  448. break;
  449. case TransitionType.Zoom:
  450. DoubleAnimation scaleXAnimation = GetScaleXAnimation(transition, 1, transition.InitialScale, transition.CloseEasingFunction);
  451. DoubleAnimation scaleYAnimation = GetScaleYAnimation(transition, 1, transition.InitialScale, transition.CloseEasingFunction);
  452. storyboard.Children.Add(scaleXAnimation);
  453. storyboard.Children.Add(scaleYAnimation);
  454. storyboard.Children.Add(progressAnimation);
  455. break;
  456. case TransitionType.ZoomX:
  457. scaleXAnimation = GetScaleXAnimation(transition, 1, transition.InitialScale, transition.CloseEasingFunction);
  458. storyboard.Children.Add(scaleXAnimation);
  459. storyboard.Children.Add(progressAnimation);
  460. break;
  461. case TransitionType.ZoomY:
  462. scaleYAnimation = GetScaleYAnimation(transition, 1, transition.InitialScale, transition.CloseEasingFunction);
  463. storyboard.Children.Add(scaleYAnimation);
  464. storyboard.Children.Add(progressAnimation);
  465. break;
  466. case TransitionType.ZoomLeft:
  467. transition.RenderTransformOrigin = new Point(0, 0.5);
  468. scaleXAnimation = GetScaleXAnimation(transition, 1, transition.InitialScale, transition.CloseEasingFunction);
  469. storyboard.Children.Add(scaleXAnimation);
  470. storyboard.Children.Add(progressAnimation);
  471. break;
  472. case TransitionType.ZoomRight:
  473. transition.RenderTransformOrigin = new Point(1, 0.5);
  474. scaleXAnimation = GetScaleXAnimation(transition, 1, transition.InitialScale, transition.CloseEasingFunction);
  475. storyboard.Children.Add(scaleXAnimation);
  476. storyboard.Children.Add(progressAnimation);
  477. break;
  478. case TransitionType.ZoomUp:
  479. transition.RenderTransformOrigin = new Point(0.5, 0);
  480. scaleYAnimation = GetScaleYAnimation(transition, 1, transition.InitialScale, transition.CloseEasingFunction);
  481. storyboard.Children.Add(scaleYAnimation);
  482. storyboard.Children.Add(progressAnimation);
  483. break;
  484. case TransitionType.ZoomDown:
  485. transition.RenderTransformOrigin = new Point(0.5, 1);
  486. scaleYAnimation = GetScaleYAnimation(transition, 1, transition.InitialScale, transition.CloseEasingFunction);
  487. storyboard.Children.Add(scaleYAnimation);
  488. storyboard.Children.Add(progressAnimation);
  489. break;
  490. }
  491. storyboard.Begin();
  492. }
  493. private static DoubleAnimation GetProgressAnimation(Transition transition, double to, IEasingFunction easing, double? from = null)
  494. {
  495. DoubleAnimation progressAnimation = new DoubleAnimation()
  496. {
  497. From = transition.Progress == to ? (to > 0 ? 0 : 1) : from,
  498. To = to,
  499. Duration = transition.Duration,
  500. EasingFunction = easing,
  501. };
  502. if (from != null)
  503. {
  504. progressAnimation.From = from;
  505. }
  506. Storyboard.SetTargetProperty(progressAnimation, new PropertyPath(ProgressProperty));
  507. Storyboard.SetTarget(progressAnimation, transition);
  508. return progressAnimation;
  509. }
  510. private static DoubleAnimation GetTranslateXAnimation(Transition transition, double from, double to, IEasingFunction easing)
  511. {
  512. DoubleAnimation transformAnimation = new DoubleAnimation()
  513. {
  514. From = from,
  515. To = to,
  516. Duration = transition.Duration,
  517. EasingFunction = easing,
  518. };
  519. Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"));
  520. Storyboard.SetTarget(transformAnimation, transition);
  521. return transformAnimation;
  522. }
  523. private static DoubleAnimation GetTranslateYAnimation(Transition transition, double from, double to, IEasingFunction easing)
  524. {
  525. DoubleAnimation transformAnimation = new DoubleAnimation()
  526. {
  527. From = from,
  528. To = to,
  529. Duration = transition.Duration,
  530. EasingFunction = easing,
  531. };
  532. Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)"));
  533. Storyboard.SetTarget(transformAnimation, transition);
  534. return transformAnimation;
  535. }
  536. private static DoubleAnimation GetScaleXAnimation(Transition transition, double from, double to, IEasingFunction easing)
  537. {
  538. DoubleAnimation transformAnimation = new DoubleAnimation()
  539. {
  540. From = from,
  541. To = to,
  542. Duration = transition.Duration,
  543. EasingFunction = easing,
  544. };
  545. Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
  546. Storyboard.SetTarget(transformAnimation, transition);
  547. return transformAnimation;
  548. }
  549. private static DoubleAnimation GetScaleYAnimation(Transition transition, double from, double to, IEasingFunction easing)
  550. {
  551. DoubleAnimation transformAnimation = new DoubleAnimation()
  552. {
  553. From = from,
  554. To = to,
  555. Duration = transition.Duration,
  556. EasingFunction = easing,
  557. };
  558. Storyboard.SetTargetProperty(transformAnimation, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));
  559. Storyboard.SetTarget(transformAnimation, transition);
  560. return transformAnimation;
  561. }
  562. private static double GetCollapsedProgress(Transition transition)
  563. {
  564. switch (transition.Type)
  565. {
  566. case TransitionType.CollapseUp:
  567. case TransitionType.CollapseDown:
  568. return transition.ActualHeight == 0 ? 0 : transition.CollapsedSize / transition.ActualHeight;
  569. case TransitionType.CollapseLeft:
  570. case TransitionType.CollapseRight:
  571. return transition.ActualWidth == 0 ? 0 : transition.CollapsedSize / transition.ActualWidth;
  572. default:
  573. return 0;
  574. }
  575. }
  576. }
  577. /// <summary>
  578. /// 转换类型
  579. /// </summary>
  580. public enum TransitionType
  581. {
  582. /// <summary>
  583. /// 无
  584. /// </summary>
  585. None = 0,
  586. /// <summary>
  587. /// 淡入
  588. /// </summary>
  589. Fade,
  590. /// <summary>
  591. /// 向左淡入
  592. /// </summary>
  593. FadeLeft,
  594. /// <summary>
  595. /// 向右淡入
  596. /// </summary>
  597. FadeRight,
  598. /// <summary>
  599. /// 向上淡入
  600. /// </summary>
  601. FadeUp,
  602. /// <summary>
  603. /// 向下淡入
  604. /// </summary>
  605. FadeDown,
  606. /// <summary>
  607. /// 缩放
  608. /// </summary>
  609. Zoom,
  610. /// <summary>
  611. /// X 轴缩放
  612. /// </summary>
  613. ZoomX,
  614. /// <summary>
  615. /// Y 轴缩放
  616. /// </summary>
  617. ZoomY,
  618. /// <summary>
  619. /// 向左缩放
  620. /// </summary>
  621. ZoomLeft,
  622. /// <summary>
  623. /// 向右缩放
  624. /// </summary>
  625. ZoomRight,
  626. /// <summary>
  627. /// 向上缩放
  628. /// </summary>
  629. ZoomUp,
  630. /// <summary>
  631. /// 向下缩放
  632. /// </summary>
  633. ZoomDown,
  634. /// <summary>
  635. /// 向上折叠
  636. /// </summary>
  637. CollapseUp,
  638. /// <summary>
  639. /// 向下折叠
  640. /// </summary>
  641. CollapseDown,
  642. /// <summary>
  643. /// 向左折叠
  644. /// </summary>
  645. CollapseLeft,
  646. /// <summary>
  647. /// 向右折叠
  648. /// </summary>
  649. CollapseRight,
  650. }
  651. }