NavigationPageAttachedMethodsPage.xaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 NavigationPageAttachedMethodsPage : UserControl
  9. {
  10. private bool _initialized;
  11. private int _pageCount;
  12. public NavigationPageAttachedMethodsPage()
  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. await DemoNav.PushAsync(new ContentPage
  23. {
  24. Header = "Root Page",
  25. Content = new StackPanel
  26. {
  27. HorizontalAlignment = HorizontalAlignment.Center,
  28. VerticalAlignment = VerticalAlignment.Center,
  29. Spacing = 12,
  30. Children =
  31. {
  32. new TextBlock
  33. {
  34. Text = "Root Page",
  35. FontSize = 24,
  36. FontWeight = FontWeight.Bold,
  37. HorizontalAlignment = HorizontalAlignment.Center,
  38. },
  39. new TextBlock
  40. {
  41. Text = "Configure per-page settings on the right,\nthen press \"Push Page with Above Settings\"\nto apply them to a new page.",
  42. FontSize = 13,
  43. TextWrapping = TextWrapping.Wrap,
  44. HorizontalAlignment = HorizontalAlignment.Center,
  45. TextAlignment = TextAlignment.Center,
  46. Opacity = 0.65,
  47. }
  48. }
  49. },
  50. HorizontalContentAlignment = HorizontalAlignment.Stretch,
  51. VerticalContentAlignment = VerticalAlignment.Stretch
  52. }, null);
  53. }
  54. private async void OnPush(object? sender, RoutedEventArgs e)
  55. {
  56. _pageCount++;
  57. var page = new ContentPage
  58. {
  59. Background = NavigationDemoHelper.GetPageBrush(_pageCount - 1),
  60. Content = new StackPanel
  61. {
  62. HorizontalAlignment = HorizontalAlignment.Center,
  63. VerticalAlignment = VerticalAlignment.Center,
  64. Spacing = 10,
  65. Children =
  66. {
  67. new TextBlock
  68. {
  69. Text = $"Page {_pageCount}",
  70. FontSize = 26,
  71. FontWeight = FontWeight.Bold,
  72. HorizontalAlignment = HorizontalAlignment.Center,
  73. },
  74. new TextBlock
  75. {
  76. Text = BuildSummary(),
  77. FontSize = 12,
  78. TextWrapping = TextWrapping.Wrap,
  79. HorizontalAlignment = HorizontalAlignment.Center,
  80. TextAlignment = TextAlignment.Center,
  81. Opacity = 0.6,
  82. }
  83. }
  84. },
  85. HorizontalContentAlignment = HorizontalAlignment.Stretch,
  86. VerticalContentAlignment = VerticalAlignment.Stretch
  87. };
  88. NavigationPage.SetHasNavigationBar(page, HasNavBarCheck.IsChecked == true);
  89. NavigationPage.SetHasBackButton(page, HasBackButtonCheck.IsChecked == true);
  90. if (BackButtonCombo.SelectedIndex > 0)
  91. {
  92. object? content = BackButtonCombo.SelectedIndex switch
  93. {
  94. 1 => (object)"← Back",
  95. 2 => "Cancel",
  96. _ => new StackPanel
  97. {
  98. Orientation = Orientation.Horizontal,
  99. Spacing = 4,
  100. VerticalAlignment = VerticalAlignment.Center,
  101. Children =
  102. {
  103. new TextBlock { Text = "✓", FontSize = 14, VerticalAlignment = VerticalAlignment.Center },
  104. new TextBlock { Text = "Done", VerticalAlignment = VerticalAlignment.Center },
  105. }
  106. }
  107. };
  108. NavigationPage.SetBackButtonContent(page, content);
  109. }
  110. if (HeaderCombo.SelectedIndex == 3)
  111. {
  112. NavigationPage.SetHasNavigationBar(page, false);
  113. }
  114. else
  115. {
  116. object? titleView = HeaderCombo.SelectedIndex switch
  117. {
  118. 1 => (object)new StackPanel
  119. {
  120. Spacing = 0,
  121. VerticalAlignment = VerticalAlignment.Center,
  122. Children =
  123. {
  124. new TextBlock { Text = $"Page {_pageCount}", FontSize = 14, FontWeight = FontWeight.SemiBold },
  125. new TextBlock { Text = "Custom subtitle", FontSize = 10, Opacity = 0.5 },
  126. }
  127. },
  128. 2 => new TextBox
  129. {
  130. PlaceholderText = "Search…",
  131. Width = 200,
  132. VerticalAlignment = VerticalAlignment.Center,
  133. FontSize = 13,
  134. },
  135. _ => (object?)$"Page {_pageCount}"
  136. };
  137. NavigationPage.SetHeader(page, titleView);
  138. }
  139. await DemoNav.PushAsync(page);
  140. }
  141. private async void OnPop(object? sender, RoutedEventArgs e) => await DemoNav.PopAsync();
  142. private async void OnPopToRoot(object? sender, RoutedEventArgs e)
  143. {
  144. await DemoNav.PopToRootAsync();
  145. _pageCount = 0;
  146. }
  147. private string BuildSummary()
  148. {
  149. var navBar = HasNavBarCheck.IsChecked == true ? "shown" : "hidden";
  150. var backBtn = HasBackButtonCheck.IsChecked == true ? "shown" : "hidden";
  151. var backLabel = BackButtonCombo.SelectedIndex switch
  152. {
  153. 1 => "← Back", 2 => "Cancel", 3 => "✓ Done", _ => "default icon"
  154. };
  155. var headerLabel = HeaderCombo.SelectedIndex switch
  156. {
  157. 1 => "Title+subtitle", 2 => "Search box", 3 => "hidden", _ => "string"
  158. };
  159. return $"NavBar: {navBar}\nBack button: {backBtn}\nBack content: {backLabel}\nHeader: {headerLabel}";
  160. }
  161. }
  162. }