TransitioningContentControlPageViewModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Avalonia;
  6. using Avalonia.Animation;
  7. using Avalonia.Media.Imaging;
  8. using Avalonia.Platform;
  9. using MiniMvvm;
  10. namespace ControlCatalog.ViewModels
  11. {
  12. public class TransitioningContentControlPageViewModel : ViewModelBase
  13. {
  14. public TransitioningContentControlPageViewModel()
  15. {
  16. var assetLoader = AvaloniaLocator.Current.GetService<IAssetLoader>();
  17. var images = new string[]
  18. {
  19. "delicate-arch-896885_640.jpg",
  20. "hirsch-899118_640.jpg",
  21. "maple-leaf-888807_640.jpg"
  22. };
  23. foreach (var image in images)
  24. {
  25. var path = $"avares://ControlCatalog/Assets/{image}";
  26. Images.Add(new Bitmap(assetLoader.Open(new Uri(path))));
  27. }
  28. SelectedImage = Images[0];
  29. SelectedTransition = PageTransitions[1];
  30. }
  31. public List<PageTransition> PageTransitions { get; } = new List<PageTransition>()
  32. {
  33. new PageTransition("None", null),
  34. new PageTransition("CrossFade", new CrossFade(TimeSpan.FromMilliseconds(500))),
  35. new PageTransition("Slide horizontally", new PageSlide(TimeSpan.FromMilliseconds(500), PageSlide.SlideAxis.Horizontal)),
  36. new PageTransition("Slide vertically", new PageSlide(TimeSpan.FromMilliseconds(500), PageSlide.SlideAxis.Vertical))
  37. };
  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. PageTransitions[1].Transition = new CrossFade(TimeSpan.FromMilliseconds(value));
  77. PageTransitions[2].Transition = new PageSlide(TimeSpan.FromMilliseconds(value), PageSlide.SlideAxis.Horizontal);
  78. PageTransitions[3].Transition = new PageSlide(TimeSpan.FromMilliseconds(value), PageSlide.SlideAxis.Vertical);
  79. }
  80. }
  81. public void NextImage()
  82. {
  83. var index = Images.IndexOf(SelectedImage) + 1;
  84. if (index >= Images.Count)
  85. {
  86. index = 0;
  87. }
  88. SelectedImage = Images[index];
  89. }
  90. public void PrevImage()
  91. {
  92. var index = Images.IndexOf(SelectedImage) - 1;
  93. if (index < 0)
  94. {
  95. index = Images.Count-1;
  96. }
  97. SelectedImage = Images[index];
  98. }
  99. }
  100. public class PageTransition : ViewModelBase
  101. {
  102. public PageTransition(string displayTitle, IPageTransition transition)
  103. {
  104. DisplayTitle = displayTitle;
  105. Transition = transition;
  106. }
  107. public string DisplayTitle { get; }
  108. private IPageTransition _Transition;
  109. /// <summary>
  110. /// Gets or sets the transition
  111. /// </summary>
  112. public IPageTransition Transition
  113. {
  114. get { return _Transition; }
  115. set { this.RaiseAndSetIfChanged(ref _Transition, value); }
  116. }
  117. public override string ToString()
  118. {
  119. return DisplayTitle;
  120. }
  121. }
  122. }