DrawerPageEventsPage.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections.Generic;
  2. using Avalonia.Controls;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Layout;
  5. using Avalonia.Media;
  6. namespace ControlCatalog.Pages
  7. {
  8. public partial class DrawerPageEventsPage : UserControl
  9. {
  10. private readonly Dictionary<string, ContentPage> _sectionPages;
  11. public DrawerPageEventsPage()
  12. {
  13. InitializeComponent();
  14. _sectionPages = new Dictionary<string, ContentPage>
  15. {
  16. ["Home"] = CreateSectionPage("Home"),
  17. ["Profile"] = CreateSectionPage("Profile"),
  18. ["Settings"] = CreateSectionPage("Settings"),
  19. };
  20. foreach (var (name, page) in _sectionPages)
  21. {
  22. var label = name;
  23. page.NavigatedTo += (_, _) => Log($"{label}: NavigatedTo");
  24. page.NavigatedFrom += (_, _) => Log($"{label}: NavigatedFrom");
  25. }
  26. }
  27. protected override void OnLoaded(RoutedEventArgs e)
  28. {
  29. base.OnLoaded(e);
  30. DemoDrawer.Opened += OnDrawerOpened;
  31. DemoDrawer.Closing += OnClosing;
  32. DemoDrawer.Closed += OnDrawerClosed;
  33. // Set Content here so the initial NavigatedTo events fire
  34. // (VisualRoot is null in the constructor, which suppresses lifecycle events).
  35. DemoDrawer.Content = _sectionPages["Home"];
  36. }
  37. protected override void OnUnloaded(RoutedEventArgs e)
  38. {
  39. base.OnUnloaded(e);
  40. DemoDrawer.Opened -= OnDrawerOpened;
  41. DemoDrawer.Closing -= OnClosing;
  42. DemoDrawer.Closed -= OnDrawerClosed;
  43. }
  44. private void OnDrawerOpened(object? sender, System.EventArgs e) => Log("Opened");
  45. private void OnDrawerClosed(object? sender, System.EventArgs e) => Log("Closed");
  46. private void OnToggle(object? sender, RoutedEventArgs e)
  47. {
  48. DemoDrawer.IsOpen = !DemoDrawer.IsOpen;
  49. }
  50. private void OnClosing(object? sender, DrawerClosingEventArgs e)
  51. {
  52. if (CancelCheck.IsChecked == true)
  53. {
  54. e.Cancel = true;
  55. CancelCheck.IsChecked = false;
  56. Log("Closing \u2192 cancelled");
  57. }
  58. else
  59. {
  60. Log("Closing");
  61. }
  62. }
  63. private void OnSelectSection(object? sender, RoutedEventArgs e)
  64. {
  65. if (sender is not Button btn) return;
  66. var section = btn.Tag?.ToString() ?? "Home";
  67. if (!_sectionPages.TryGetValue(section, out var page)) return;
  68. if (ReferenceEquals(DemoDrawer.Content, page))
  69. {
  70. DemoDrawer.IsOpen = false;
  71. return;
  72. }
  73. Log($"\u2192 {section}");
  74. DemoDrawer.Content = page;
  75. DemoDrawer.IsOpen = false;
  76. }
  77. private void OnClearLog(object? sender, RoutedEventArgs e)
  78. {
  79. EventLog.Text = string.Empty;
  80. }
  81. private void Log(string message)
  82. {
  83. EventLog.Text = $"{message}\n{EventLog.Text}";
  84. }
  85. private static ContentPage CreateSectionPage(string header) => new ContentPage
  86. {
  87. Header = header,
  88. Content = new StackPanel
  89. {
  90. HorizontalAlignment = HorizontalAlignment.Center,
  91. VerticalAlignment = VerticalAlignment.Center,
  92. Spacing = 8,
  93. Children =
  94. {
  95. new TextBlock
  96. {
  97. Text = header,
  98. FontSize = 24,
  99. FontWeight = FontWeight.SemiBold,
  100. HorizontalAlignment = HorizontalAlignment.Center,
  101. },
  102. new TextBlock
  103. {
  104. Text = "Tap a drawer item to navigate.\nWatch the event log in the panel.",
  105. TextWrapping = TextWrapping.Wrap,
  106. Opacity = 0.6,
  107. TextAlignment = TextAlignment.Center,
  108. FontSize = 13,
  109. }
  110. }
  111. }
  112. };
  113. }
  114. }