using System; using System.Collections.Generic; using System.IO; using System.Text; using Avalonia; using Avalonia.Animation; using Avalonia.Media.Imaging; using Avalonia.Platform; using MiniMvvm; namespace ControlCatalog.ViewModels { public class TransitioningContentControlPageViewModel : ViewModelBase { public TransitioningContentControlPageViewModel() { var assetLoader = AvaloniaLocator.Current.GetService(); var images = new string[] { "delicate-arch-896885_640.jpg", "hirsch-899118_640.jpg", "maple-leaf-888807_640.jpg" }; foreach (var image in images) { var path = $"avares://ControlCatalog/Assets/{image}"; Images.Add(new Bitmap(assetLoader.Open(new Uri(path)))); } SelectedImage = Images[0]; SelectedTransition = PageTransitions[1]; } public List PageTransitions { get; } = new List() { new PageTransition("None", null), new PageTransition("CrossFade", new CrossFade(TimeSpan.FromMilliseconds(500))), new PageTransition("Slide horizontally", new PageSlide(TimeSpan.FromMilliseconds(500), PageSlide.SlideAxis.Horizontal)), new PageTransition("Slide vertically", new PageSlide(TimeSpan.FromMilliseconds(500), PageSlide.SlideAxis.Vertical)) }; public List Images { get; } = new List(); private Bitmap? _SelectedImage; /// /// Gets or Sets the selected image /// public Bitmap? SelectedImage { get { return _SelectedImage; } set { this.RaiseAndSetIfChanged(ref _SelectedImage, value); } } private PageTransition _SelectedTransition; /// /// Gets or sets the transition to play /// public PageTransition SelectedTransition { get { return _SelectedTransition; } set { this.RaiseAndSetIfChanged(ref _SelectedTransition, value); } } private bool _ClipToBounds; /// /// Gets or sets if the content should be clipped to bounds /// public bool ClipToBounds { get { return _ClipToBounds; } set { this.RaiseAndSetIfChanged(ref _ClipToBounds, value); } } private int _Duration = 500; /// /// Gets or Sets the duration /// public int Duration { get { return _Duration; } set { this.RaiseAndSetIfChanged(ref _Duration , value); PageTransitions[1].Transition = new CrossFade(TimeSpan.FromMilliseconds(value)); PageTransitions[2].Transition = new PageSlide(TimeSpan.FromMilliseconds(value), PageSlide.SlideAxis.Horizontal); PageTransitions[3].Transition = new PageSlide(TimeSpan.FromMilliseconds(value), PageSlide.SlideAxis.Vertical); } } public void NextImage() { var index = Images.IndexOf(SelectedImage) + 1; if (index >= Images.Count) { index = 0; } SelectedImage = Images[index]; } public void PrevImage() { var index = Images.IndexOf(SelectedImage) - 1; if (index < 0) { index = Images.Count-1; } SelectedImage = Images[index]; } } public class PageTransition : ViewModelBase { public PageTransition(string displayTitle, IPageTransition transition) { DisplayTitle = displayTitle; Transition = transition; } public string DisplayTitle { get; } private IPageTransition _Transition; /// /// Gets or sets the transition /// public IPageTransition Transition { get { return _Transition; } set { this.RaiseAndSetIfChanged(ref _Transition, value); } } public override string ToString() { return DisplayTitle; } } }