TransitioningContentControlPageViewModel.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 assetLoader = AvaloniaLocator.Current?.GetService<IAssetLoader>()!;
  22. var images = new string[]
  23. {
  24. "delicate-arch-896885_640.jpg",
  25. "hirsch-899118_640.jpg",
  26. "maple-leaf-888807_640.jpg"
  27. };
  28. foreach (var image in images)
  29. {
  30. var path = $"avares://ControlCatalog/Assets/{image}";
  31. Images.Add(new Bitmap(assetLoader.Open(new Uri(path))));
  32. }
  33. SetupTransitions();
  34. _SelectedTransition = PageTransitions[1];
  35. _SelectedImage = Images[0];
  36. }
  37. public List<PageTransition> PageTransitions { get; } = new List<PageTransition>();
  38. public List<Bitmap> Images { get; } = new List<Bitmap>();
  39. private Bitmap _SelectedImage;
  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. private void SetupTransitions()
  80. {
  81. if (PageTransitions.Count == 0)
  82. {
  83. PageTransitions.AddRange(new[]
  84. {
  85. new PageTransition("None"),
  86. new PageTransition("CrossFade"),
  87. new PageTransition("Slide horizontally"),
  88. new PageTransition("Slide vertically"),
  89. new PageTransition("Composite"),
  90. new PageTransition("Custom")
  91. });
  92. }
  93. PageTransitions[1].Transition = new CrossFade(TimeSpan.FromMilliseconds(Duration));
  94. PageTransitions[2].Transition = new PageSlide(TimeSpan.FromMilliseconds(Duration), PageSlide.SlideAxis.Horizontal);
  95. PageTransitions[3].Transition = new PageSlide(TimeSpan.FromMilliseconds(Duration), PageSlide.SlideAxis.Vertical);
  96. var compositeTransition = new CompositePageTransition();
  97. compositeTransition.PageTransitions.Add(PageTransitions[1].Transition);
  98. compositeTransition.PageTransitions.Add(PageTransitions[2].Transition);
  99. compositeTransition.PageTransitions.Add(PageTransitions[3].Transition);
  100. PageTransitions[4].Transition = compositeTransition;
  101. PageTransitions[5].Transition = new CustomTransition(TimeSpan.FromMilliseconds(Duration));
  102. }
  103. public void NextImage()
  104. {
  105. var index = Images.IndexOf(SelectedImage) + 1;
  106. if (index >= Images.Count)
  107. {
  108. index = 0;
  109. }
  110. SelectedImage = Images[index];
  111. }
  112. public void PrevImage()
  113. {
  114. var index = Images.IndexOf(SelectedImage) - 1;
  115. if (index < 0)
  116. {
  117. index = Images.Count-1;
  118. }
  119. SelectedImage = Images[index];
  120. }
  121. }
  122. public class PageTransition : ViewModelBase
  123. {
  124. public PageTransition(string displayTitle)
  125. {
  126. DisplayTitle = displayTitle;
  127. }
  128. public string DisplayTitle { get; }
  129. private IPageTransition? _Transition;
  130. /// <summary>
  131. /// Gets or sets the transition
  132. /// </summary>
  133. public IPageTransition? Transition
  134. {
  135. get { return _Transition; }
  136. set { this.RaiseAndSetIfChanged(ref _Transition, value); }
  137. }
  138. public override string ToString()
  139. {
  140. return DisplayTitle;
  141. }
  142. }
  143. public class CustomTransition : IPageTransition
  144. {
  145. /// <summary>
  146. /// Initializes a new instance of the <see cref="CustomTransition"/> class.
  147. /// </summary>
  148. public CustomTransition()
  149. {
  150. }
  151. /// <summary>
  152. /// Initializes a new instance of the <see cref="CustomTransition"/> class.
  153. /// </summary>
  154. /// <param name="duration">The duration of the animation.</param>
  155. public CustomTransition(TimeSpan duration)
  156. {
  157. Duration = duration;
  158. }
  159. /// <summary>
  160. /// Gets the duration of the animation.
  161. /// </summary>
  162. public TimeSpan Duration { get; set; }
  163. public async Task Start(Visual? from, Visual? to, bool forward, CancellationToken cancellationToken)
  164. {
  165. if (cancellationToken.IsCancellationRequested)
  166. {
  167. return;
  168. }
  169. var tasks = new List<Task>();
  170. var parent = GetVisualParent(from, to);
  171. var scaleProperty = ScaleTransform.ScaleYProperty;
  172. if (from != null)
  173. {
  174. var animation = new Animation
  175. {
  176. FillMode = FillMode.Forward,
  177. Children =
  178. {
  179. new KeyFrame
  180. {
  181. Setters = { new Setter { Property = scaleProperty, Value = 1d } },
  182. Cue = new Cue(0d)
  183. },
  184. new KeyFrame
  185. {
  186. Setters =
  187. {
  188. new Setter
  189. {
  190. Property = scaleProperty,
  191. Value = 0d
  192. }
  193. },
  194. Cue = new Cue(1d)
  195. }
  196. },
  197. Duration = Duration
  198. };
  199. tasks.Add(animation.RunAsync(from, null, cancellationToken));
  200. }
  201. if (to != null)
  202. {
  203. to.IsVisible = true;
  204. var animation = new Animation
  205. {
  206. FillMode = FillMode.Forward,
  207. Children =
  208. {
  209. new KeyFrame
  210. {
  211. Setters =
  212. {
  213. new Setter
  214. {
  215. Property = scaleProperty,
  216. Value = 0d
  217. }
  218. },
  219. Cue = new Cue(0d)
  220. },
  221. new KeyFrame
  222. {
  223. Setters = { new Setter { Property = scaleProperty, Value = 1d } },
  224. Cue = new Cue(1d)
  225. }
  226. },
  227. Duration = Duration
  228. };
  229. tasks.Add(animation.RunAsync(to, null, cancellationToken));
  230. }
  231. await Task.WhenAll(tasks);
  232. if (from != null && !cancellationToken.IsCancellationRequested)
  233. {
  234. from.IsVisible = false;
  235. }
  236. }
  237. /// <summary>
  238. /// Gets the common visual parent of the two control.
  239. /// </summary>
  240. /// <param name="from">The from control.</param>
  241. /// <param name="to">The to control.</param>
  242. /// <returns>The common parent.</returns>
  243. /// <exception cref="ArgumentException">
  244. /// The two controls do not share a common parent.
  245. /// </exception>
  246. /// <remarks>
  247. /// Any one of the parameters may be null, but not both.
  248. /// </remarks>
  249. private static IVisual GetVisualParent(IVisual? from, IVisual? to)
  250. {
  251. var p1 = (from ?? to)!.VisualParent;
  252. var p2 = (to ?? from)!.VisualParent;
  253. if (p1 != null && p2 != null && p1 != p2)
  254. {
  255. throw new ArgumentException("Controls for PageSlide must have same parent.");
  256. }
  257. return p1 ?? throw new InvalidOperationException("Cannot determine visual parent.");
  258. }
  259. }
  260. }