CarouselPageCustomizationPage.xaml.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections;
  3. using Avalonia.Animation;
  4. using Avalonia.Controls;
  5. using Avalonia.Interactivity;
  6. namespace ControlCatalog.Pages
  7. {
  8. public partial class CarouselPageCustomizationPage : UserControl
  9. {
  10. public CarouselPageCustomizationPage()
  11. {
  12. InitializeComponent();
  13. Loaded += OnLoaded;
  14. Unloaded += OnUnloaded;
  15. }
  16. private void OnLoaded(object? sender, RoutedEventArgs e)
  17. {
  18. DemoCarousel.PageTransition = new PageSlide(TimeSpan.FromMilliseconds(300), PageSlide.SlideAxis.Horizontal);
  19. DemoCarousel.SelectionChanged += OnSelectionChanged;
  20. UpdateDots(DemoCarousel.SelectedIndex);
  21. UpdateStatus();
  22. }
  23. private void OnUnloaded(object? sender, RoutedEventArgs e)
  24. {
  25. DemoCarousel.SelectionChanged -= OnSelectionChanged;
  26. }
  27. private void OnSelectionChanged(object? sender, PageSelectionChangedEventArgs e)
  28. {
  29. UpdateDots(DemoCarousel.SelectedIndex);
  30. UpdateStatus();
  31. }
  32. private void OnOrientationChanged(object? sender, SelectionChangedEventArgs e)
  33. {
  34. if (DemoCarousel == null)
  35. return;
  36. var axis = OrientationCombo.SelectedIndex == 1
  37. ? PageSlide.SlideAxis.Vertical
  38. : PageSlide.SlideAxis.Horizontal;
  39. DemoCarousel.PageTransition = new PageSlide(TimeSpan.FromMilliseconds(300), axis);
  40. UpdateStatus();
  41. }
  42. private void OnPrevious(object? sender, RoutedEventArgs e)
  43. {
  44. if (DemoCarousel.SelectedIndex > 0)
  45. DemoCarousel.SelectedIndex--;
  46. }
  47. private void OnNext(object? sender, RoutedEventArgs e)
  48. {
  49. var pageCount = (DemoCarousel.Pages as IList)?.Count ?? 0;
  50. if (DemoCarousel.SelectedIndex < pageCount - 1)
  51. DemoCarousel.SelectedIndex++;
  52. }
  53. private void UpdateDots(int selectedIndex)
  54. {
  55. Dot0.Opacity = selectedIndex == 0 ? 1.0 : 0.4;
  56. Dot1.Opacity = selectedIndex == 1 ? 1.0 : 0.4;
  57. Dot2.Opacity = selectedIndex == 2 ? 1.0 : 0.4;
  58. Dot3.Opacity = selectedIndex == 3 ? 1.0 : 0.4;
  59. }
  60. private void UpdateStatus()
  61. {
  62. if (StatusText == null) return;
  63. var pageCount = (DemoCarousel.Pages as IList)?.Count ?? 0;
  64. var axis = OrientationCombo?.SelectedIndex == 1 ? "Vertical" : "Horizontal";
  65. StatusText.Text = $"Page {DemoCarousel.SelectedIndex + 1} of {pageCount} | {axis}";
  66. }
  67. }
  68. }