NavigationPageBackButtonPage.xaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Threading.Tasks;
  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 NavigationPageBackButtonPage : UserControl
  9. {
  10. private bool _initialized;
  11. private int _pushCount;
  12. public NavigationPageBackButtonPage()
  13. {
  14. InitializeComponent();
  15. Loaded += OnLoaded;
  16. }
  17. private async void OnLoaded(object? sender, RoutedEventArgs e)
  18. {
  19. if (_initialized)
  20. return;
  21. _initialized = true;
  22. DemoNav.Pushed += (s, ev) => AddLog($"Pushed: \"{ev.Page?.Header}\"");
  23. DemoNav.Popped += (s, ev) => AddLog($"Popped: \"{ev.Page?.Header}\"");
  24. await DemoNav.PushAsync(CreatePage("Home", "This is the root page.\nNo back button is shown here.\n\nPush pages from the config panel\nto explore back button behaviors.", null), null);
  25. }
  26. private void OnGlobalBackButtonChanged(object? sender, RoutedEventArgs e)
  27. {
  28. if (DemoNav == null)
  29. return;
  30. DemoNav.IsBackButtonVisible = IsBackButtonVisibleCheck.IsChecked == true;
  31. AddLog($"IsBackButtonVisible={DemoNav.IsBackButtonVisible}");
  32. }
  33. private async void OnPushStandard(object? sender, RoutedEventArgs e)
  34. {
  35. var page = CreatePage($"Page {_pushCount + 1}", "Standard page with default back arrow.", null);
  36. await DemoNav.PushAsync(page);
  37. }
  38. private async void OnPushNoBack(object? sender, RoutedEventArgs e)
  39. {
  40. var page = CreatePage($"No Back #{_pushCount + 1}", "IsBackButtonVisible = false\n\nThe back arrow is hidden.\nUse the Pop button to go back.", null);
  41. NavigationPage.SetHasBackButton(page, false);
  42. await DemoNav.PushAsync(page);
  43. AddLog($"HasBackButton=false on \"{page.Header}\"");
  44. }
  45. private async void OnPushDisabledBack(object? sender, RoutedEventArgs e)
  46. {
  47. var page = CreatePage($"Disabled Back #{_pushCount + 1}", "IsBackButtonEnabled = false\n\nThe back arrow is visible but disabled.\nUse the Pop button to go back.", null);
  48. NavigationPage.SetIsBackButtonEnabled(page, false);
  49. await DemoNav.PushAsync(page);
  50. AddLog($"IsBackButtonEnabled=false on \"{page.Header}\"");
  51. }
  52. private async void OnPushCustomText(object? sender, RoutedEventArgs e)
  53. {
  54. var text = string.IsNullOrWhiteSpace(BackContentInput?.Text) ? "Cancel" : BackContentInput!.Text;
  55. var page = CreatePage($"Text Back #{_pushCount + 1}", $"BackButtonContent = \"{text}\"\n\nThe back button shows custom text.", null);
  56. NavigationPage.SetBackButtonContent(page, text);
  57. await DemoNav.PushAsync(page);
  58. AddLog($"BackButtonContent=\"{text}\" on \"{page.Header}\"");
  59. }
  60. private async void OnPushCustomIcon(object? sender, RoutedEventArgs e)
  61. {
  62. var page = CreatePage($"Icon Back #{_pushCount + 1}", "BackButtonContent = PathIcon (x)\n\nThe back button shows a custom icon.", null);
  63. NavigationPage.SetBackButtonContent(page, new TextBlock
  64. {
  65. Text = "\u2715",
  66. FontSize = 16,
  67. VerticalAlignment = VerticalAlignment.Center
  68. });
  69. await DemoNav.PushAsync(page);
  70. AddLog($"BackButtonContent=icon on \"{page.Header}\"");
  71. }
  72. private async void OnPushIconTextBack(object? sender, RoutedEventArgs e)
  73. {
  74. var page = CreatePage($"Icon+Text Back #{_pushCount + 1}", "BackButtonContent = icon + text\n\nThe back button shows both icon and text.", null);
  75. var content = new StackPanel
  76. {
  77. Orientation = Orientation.Horizontal,
  78. Spacing = 4,
  79. VerticalAlignment = VerticalAlignment.Center,
  80. Children =
  81. {
  82. new TextBlock
  83. {
  84. Text = "\u2715",
  85. FontSize = 14,
  86. VerticalAlignment = VerticalAlignment.Center,
  87. },
  88. new TextBlock
  89. {
  90. Text = "Close",
  91. VerticalAlignment = VerticalAlignment.Center,
  92. FontSize = 14,
  93. },
  94. }
  95. };
  96. NavigationPage.SetBackButtonContent(page, content);
  97. await DemoNav.PushAsync(page);
  98. AddLog($"BackButtonContent=icon+text on \"{page.Header}\"");
  99. }
  100. private async void OnPushGuarded(object? sender, RoutedEventArgs e)
  101. {
  102. var useAsync = DeferRadio?.IsChecked == true;
  103. var mode = useAsync ? "async save" : "cancel";
  104. var page = CreatePage($"Guarded #{_pushCount + 1}",
  105. useAsync
  106. ? "This page uses an async Navigating handler.\n\nWhen you tap back, it simulates\nan async save (1.5s) before\nallowing the navigation."
  107. : "This page cancels back navigation.\n\nTapping back will be blocked.\nUse the Pop button to force-pop.",
  108. Color.Parse("#FCE4EC"));
  109. page.Navigating += async (args) =>
  110. {
  111. if (args.NavigationType != NavigationType.Pop) return;
  112. if (useAsync)
  113. {
  114. AddLog("Saving...");
  115. await Task.Delay(1500);
  116. AddLog("Saved, navigation allowed");
  117. }
  118. else
  119. {
  120. args.Cancel = true;
  121. AddLog("Navigation CANCELLED");
  122. }
  123. };
  124. await DemoNav.PushAsync(page);
  125. AddLog($"Guarded page ({mode}) pushed");
  126. }
  127. private async void OnPop(object? sender, RoutedEventArgs e) => await DemoNav.PopAsync();
  128. private async void OnPopToRoot(object? sender, RoutedEventArgs e) => await DemoNav.PopToRootAsync();
  129. private void OnClearLog(object? sender, RoutedEventArgs e)
  130. {
  131. LogPanel.Children.Clear();
  132. }
  133. private void AddLog(string message)
  134. {
  135. LogPanel.Children.Insert(0, new TextBlock
  136. {
  137. Text = message,
  138. FontFamily = new FontFamily("Cascadia Code,Consolas,Menlo,monospace"),
  139. FontSize = 10,
  140. TextWrapping = TextWrapping.Wrap,
  141. });
  142. }
  143. private ContentPage CreatePage(string title, string body, Color? bg)
  144. {
  145. _pushCount++;
  146. var page = NavigationDemoHelper.MakePage(title, body, _pushCount);
  147. if (bg.HasValue)
  148. page.Background = new SolidColorBrush(bg.Value);
  149. return page;
  150. }
  151. }
  152. }