TransitioningContentControlPageViewModel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Avalonia;
  8. using Avalonia.Animation;
  9. using Avalonia.Media;
  10. using Avalonia.Media.Imaging;
  11. using Avalonia.Platform;
  12. using Avalonia.Styling;
  13. using Avalonia.VisualTree;
  14. using MiniMvvm;
  15. namespace ControlCatalog.ViewModels
  16. {
  17. public class TransitioningContentControlPageViewModel : ViewModelBase
  18. {
  19. public TransitioningContentControlPageViewModel()
  20. {
  21. var images = new string[]
  22. {
  23. "delicate-arch-896885_640.jpg",
  24. "hirsch-899118_640.jpg",
  25. "maple-leaf-888807_640.jpg"
  26. };
  27. foreach (var image in images)
  28. {
  29. var path = $"avares://ControlCatalog/Assets/{image}";
  30. Images.Add(new Bitmap(AssetLoader.Open(new Uri(path))));
  31. }
  32. SetupTransitions();
  33. _SelectedTransition = PageTransitions[1];
  34. _SelectedImage = Images[0];
  35. }
  36. public List<PageTransition> PageTransitions { get; } = new List<PageTransition>();
  37. public List<Bitmap> Images { get; } = new List<Bitmap>();
  38. private Bitmap _SelectedImage;
  39. private bool _Reversed;
  40. /// <summary>
  41. /// Gets or Sets the selected image
  42. /// </summary>
  43. public Bitmap SelectedImage
  44. {
  45. get { return _SelectedImage; }
  46. set { this.RaiseAndSetIfChanged(ref _SelectedImage, value); }
  47. }
  48. private PageTransition _SelectedTransition;
  49. /// <summary>
  50. /// Gets or sets the transition to play
  51. /// </summary>
  52. public PageTransition SelectedTransition
  53. {
  54. get { return _SelectedTransition; }
  55. set { this.RaiseAndSetIfChanged(ref _SelectedTransition, value); }
  56. }
  57. private bool _ClipToBounds;
  58. /// <summary>
  59. /// Gets or sets if the content should be clipped to bounds
  60. /// </summary>
  61. public bool ClipToBounds
  62. {
  63. get { return _ClipToBounds; }
  64. set { this.RaiseAndSetIfChanged(ref _ClipToBounds, value); }
  65. }
  66. private int _Duration = 500;
  67. /// <summary>
  68. /// Gets or Sets the duration
  69. /// </summary>
  70. public int Duration
  71. {
  72. get { return _Duration; }
  73. set
  74. {
  75. this.RaiseAndSetIfChanged(ref _Duration , value);
  76. SetupTransitions();
  77. }
  78. }
  79. /// <summary>
  80. /// Gets or sets a value indicating whether the animation is reversed.
  81. /// </summary>
  82. public bool Reversed
  83. {
  84. get => _Reversed;
  85. set => this.RaiseAndSetIfChanged(ref _Reversed, value);
  86. }
  87. private void SetupTransitions()
  88. {
  89. if (PageTransitions.Count == 0)
  90. {
  91. PageTransitions.AddRange(new[]
  92. {
  93. new PageTransition("None"),
  94. new PageTransition("CrossFade"),
  95. new PageTransition("Slide horizontally"),
  96. new PageTransition("Slide vertically"),
  97. new PageTransition("Composite"),
  98. new PageTransition("Custom")
  99. });
  100. }
  101. PageTransitions[1].Transition = new CrossFade(TimeSpan.FromMilliseconds(Duration));
  102. PageTransitions[2].Transition = new PageSlide(TimeSpan.FromMilliseconds(Duration), PageSlide.SlideAxis.Horizontal);
  103. PageTransitions[3].Transition = new PageSlide(TimeSpan.FromMilliseconds(Duration), PageSlide.SlideAxis.Vertical);
  104. var compositeTransition = new CompositePageTransition();
  105. compositeTransition.PageTransitions.Add(PageTransitions[1].Transition!);
  106. compositeTransition.PageTransitions.Add(PageTransitions[2].Transition!);
  107. compositeTransition.PageTransitions.Add(PageTransitions[3].Transition!);
  108. PageTransitions[4].Transition = compositeTransition;
  109. PageTransitions[5].Transition = new CustomTransition(TimeSpan.FromMilliseconds(Duration));
  110. }
  111. public void NextImage()
  112. {
  113. Reversed = false;
  114. var index = Images.IndexOf(SelectedImage) + 1;
  115. if (index >= Images.Count)
  116. {
  117. index = 0;
  118. }
  119. SelectedImage = Images[index];
  120. }
  121. public void PrevImage()
  122. {
  123. Reversed = true;
  124. var index = Images.IndexOf(SelectedImage) - 1;
  125. if (index < 0)
  126. {
  127. index = Images.Count-1;
  128. }
  129. SelectedImage = Images[index];
  130. }
  131. }
  132. public class PageTransition : ViewModelBase
  133. {
  134. public PageTransition(string displayTitle)
  135. {
  136. DisplayTitle = displayTitle;
  137. }
  138. public string DisplayTitle { get; }
  139. private IPageTransition? _Transition;
  140. /// <summary>
  141. /// Gets or sets the transition
  142. /// </summary>
  143. public IPageTransition? Transition
  144. {
  145. get { return _Transition; }
  146. set { this.RaiseAndSetIfChanged(ref _Transition, value); }
  147. }
  148. public override string ToString()
  149. {
  150. return DisplayTitle;
  151. }
  152. }
  153. public class CustomTransition : IPageTransition
  154. {
  155. /// <summary>
  156. /// Initializes a new instance of the <see cref="CustomTransition"/> class.
  157. /// </summary>
  158. public CustomTransition()
  159. {
  160. }
  161. /// <summary>
  162. /// Initializes a new instance of the <see cref="CustomTransition"/> class.
  163. /// </summary>
  164. /// <param name="duration">The duration of the animation.</param>
  165. public CustomTransition(TimeSpan duration)
  166. {
  167. Duration = duration;
  168. }
  169. /// <summary>
  170. /// Gets the duration of the animation.
  171. /// </summary>
  172. public TimeSpan Duration { get; set; }
  173. public async Task Start(Visual? from, Visual? to, bool forward, CancellationToken cancellationToken)
  174. {
  175. if (cancellationToken.IsCancellationRequested)
  176. {
  177. return;
  178. }
  179. var tasks = new List<Task>();
  180. var parent = GetVisualParent(from, to);
  181. var scaleProperty = ScaleTransform.ScaleYProperty;
  182. if (from != null)
  183. {
  184. var animation = new Animation
  185. {
  186. Children =
  187. {
  188. new KeyFrame
  189. {
  190. Setters = { new Setter { Property = scaleProperty, Value = 1d } },
  191. Cue = new Cue(0d)
  192. },
  193. new KeyFrame
  194. {
  195. Setters =
  196. {
  197. new Setter
  198. {
  199. Property = scaleProperty,
  200. Value = 0d
  201. }
  202. },
  203. Cue = new Cue(1d)
  204. }
  205. },
  206. Duration = Duration
  207. };
  208. tasks.Add(animation.RunAsync(from, cancellationToken));
  209. }
  210. if (to != null)
  211. {
  212. to.IsVisible = true;
  213. var animation = new Animation
  214. {
  215. Children =
  216. {
  217. new KeyFrame
  218. {
  219. Setters =
  220. {
  221. new Setter
  222. {
  223. Property = scaleProperty,
  224. Value = 0d
  225. }
  226. },
  227. Cue = new Cue(0d)
  228. },
  229. new KeyFrame
  230. {
  231. Setters = { new Setter { Property = scaleProperty, Value = 1d } },
  232. Cue = new Cue(1d)
  233. }
  234. },
  235. Duration = Duration
  236. };
  237. tasks.Add(animation.RunAsync(to, cancellationToken));
  238. }
  239. await Task.WhenAll(tasks);
  240. if (from != null && !cancellationToken.IsCancellationRequested)
  241. {
  242. from.IsVisible = false;
  243. }
  244. }
  245. /// <summary>
  246. /// Gets the common visual parent of the two control.
  247. /// </summary>
  248. /// <param name="from">The from control.</param>
  249. /// <param name="to">The to control.</param>
  250. /// <returns>The common parent.</returns>
  251. /// <exception cref="ArgumentException">
  252. /// The two controls do not share a common parent.
  253. /// </exception>
  254. /// <remarks>
  255. /// Any one of the parameters may be null, but not both.
  256. /// </remarks>
  257. private static Visual GetVisualParent(Visual? from, Visual? to)
  258. {
  259. var p1 = (from ?? to)!.GetVisualParent();
  260. var p2 = (to ?? from)!.GetVisualParent();
  261. if (p1 != null && p2 != null && p1 != p2)
  262. {
  263. throw new ArgumentException("Controls for PageSlide must have same parent.");
  264. }
  265. return p1 ?? throw new InvalidOperationException("Cannot determine visual parent.");
  266. }
  267. }
  268. }