TabbedPageProgrammaticPage.xaml.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System.Collections.Generic;
  2. using Avalonia.Controls;
  3. using Avalonia.Interactivity;
  4. namespace ControlCatalog.Pages
  5. {
  6. public partial class TabbedPageProgrammaticPage : UserControl
  7. {
  8. private readonly List<string> _log = new();
  9. public TabbedPageProgrammaticPage()
  10. {
  11. InitializeComponent();
  12. }
  13. private void OnSelectionChanged(object? sender, PageSelectionChangedEventArgs e)
  14. {
  15. UpdateCurrentLabel();
  16. var from = (e.PreviousPage as ContentPage)?.Header?.ToString() ?? "—";
  17. var to = (e.CurrentPage as ContentPage)?.Header?.ToString() ?? "—";
  18. _log.Insert(0, $"{from} \u2192 {to}");
  19. if (_log.Count > 6) _log.RemoveAt(_log.Count - 1);
  20. if (SelectionLog != null)
  21. SelectionLog.Text = string.Join("\n", _log);
  22. }
  23. private void UpdateCurrentLabel()
  24. {
  25. if (CurrentTabLabel == null || DemoTabs == null) return;
  26. var idx = DemoTabs.SelectedIndex;
  27. var name = (DemoTabs.SelectedPage as ContentPage)?.Header?.ToString() ?? "—";
  28. CurrentTabLabel.Text = $"Index {idx} | {name}";
  29. }
  30. private void OnJumpTo(object? sender, RoutedEventArgs e)
  31. {
  32. if (sender is Button btn && int.TryParse(btn.Tag?.ToString(), out int index))
  33. DemoTabs.SelectedIndex = index;
  34. }
  35. private void OnPrevious(object? sender, RoutedEventArgs e)
  36. {
  37. if (DemoTabs.SelectedIndex > 0) DemoTabs.SelectedIndex--;
  38. }
  39. private void OnNext(object? sender, RoutedEventArgs e)
  40. {
  41. if (DemoTabs.SelectedIndex < 3) DemoTabs.SelectedIndex++;
  42. }
  43. }
  44. }