ContentDemoPage.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using Avalonia.Interactivity;
  5. using Avalonia.Layout;
  6. using Avalonia.Media;
  7. namespace ControlCatalog.Pages
  8. {
  9. public partial class ContentDemoPage : ContentPage
  10. {
  11. private static readonly (string Group, string Title, string Description, Func<UserControl> Factory)[] Demos =
  12. {
  13. // Overview
  14. ("Overview", "First Look", "Basic ContentPage with header, content, and background inside a NavigationPage.", () => new ContentPageFirstLookPage()),
  15. // Appearance
  16. ("Appearance", "Customization", "Adjust content alignment, background color, and padding to understand how ContentPage adapts its layout.", () => new ContentPageCustomizationPage()),
  17. // Features
  18. ("Features", "CommandBar", "Attach a CommandBar to the top or bottom of a ContentPage. Add and remove items at runtime.", () => new ContentPageCommandBarPage()),
  19. ("Features", "Safe Area", "Understand how AutomaticallyApplySafeAreaPadding absorbs platform insets.", () => new ContentPageSafeAreaPage()),
  20. ("Features", "Events", "Observe page lifecycle events: NavigatedTo, NavigatedFrom, and Navigating.", () => new ContentPageEventsPage()),
  21. // Performance
  22. ("Performance", "Performance Monitor", "Push ContentPages of varying weight (50 KB to 2 MB) and observe live instance count and managed heap. Pop pages and force GC to confirm memory is released.", () => new ContentPagePerformancePage()),
  23. };
  24. public ContentDemoPage()
  25. {
  26. InitializeComponent();
  27. Loaded += OnLoaded;
  28. }
  29. private async void OnLoaded(object? sender, RoutedEventArgs e)
  30. {
  31. await SampleNav.PushAsync(CreateHomePage(), null);
  32. }
  33. private ContentPage CreateHomePage()
  34. {
  35. var stack = new StackPanel
  36. {
  37. Margin = new Avalonia.Thickness(12),
  38. Spacing = 16
  39. };
  40. var groups = new Dictionary<string, WrapPanel>();
  41. var groupOrder = new List<string>();
  42. foreach (var (group, title, description, factory) in Demos)
  43. {
  44. if (!groups.ContainsKey(group))
  45. {
  46. groups[group] = new WrapPanel
  47. {
  48. Orientation = Orientation.Horizontal,
  49. HorizontalAlignment = HorizontalAlignment.Left
  50. };
  51. groupOrder.Add(group);
  52. }
  53. var demoFactory = factory;
  54. var demoTitle = title;
  55. var card = new Button
  56. {
  57. Width = 170,
  58. MinHeight = 80,
  59. Margin = new Avalonia.Thickness(0, 0, 8, 8),
  60. VerticalAlignment = VerticalAlignment.Top,
  61. HorizontalContentAlignment = HorizontalAlignment.Left,
  62. VerticalContentAlignment = VerticalAlignment.Top,
  63. Padding = new Avalonia.Thickness(12, 8),
  64. Content = new StackPanel
  65. {
  66. Spacing = 4,
  67. Children =
  68. {
  69. new TextBlock
  70. {
  71. Text = title,
  72. FontSize = 13,
  73. FontWeight = FontWeight.SemiBold,
  74. TextWrapping = TextWrapping.Wrap
  75. },
  76. new TextBlock
  77. {
  78. Text = description,
  79. FontSize = 11,
  80. Opacity = 0.6,
  81. TextWrapping = TextWrapping.Wrap
  82. }
  83. }
  84. }
  85. };
  86. card.Click += async (s, e) =>
  87. {
  88. var headerGrid = new Grid { ColumnDefinitions = new ColumnDefinitions("*, Auto") };
  89. headerGrid.Children.Add(new TextBlock
  90. {
  91. Text = demoTitle,
  92. VerticalAlignment = VerticalAlignment.Center
  93. });
  94. var closeBtn = new Button
  95. {
  96. Content = new PathIcon
  97. {
  98. Data = Geometry.Parse("M4.397 4.397a1 1 0 0 1 1.414 0L12 10.585l6.19-6.188a1 1 0 0 1 1.414 1.414L13.413 12l6.19 6.189a1 1 0 0 1-1.414 1.414L12 13.413l-6.189 6.19a1 1 0 0 1-1.414-1.414L10.585 12 4.397 5.811a1 1 0 0 1 0-1.414z")
  99. },
  100. Background = Brushes.Transparent,
  101. BorderThickness = new Avalonia.Thickness(0),
  102. Padding = new Avalonia.Thickness(8, 4),
  103. VerticalAlignment = VerticalAlignment.Center
  104. };
  105. Grid.SetColumn(closeBtn, 1);
  106. headerGrid.Children.Add(closeBtn);
  107. closeBtn.Click += async (_, _) => await SampleNav.PopAsync(null);
  108. var page = new ContentPage
  109. {
  110. Header = headerGrid,
  111. Content = demoFactory(),
  112. HorizontalContentAlignment = HorizontalAlignment.Stretch,
  113. VerticalContentAlignment = VerticalAlignment.Stretch
  114. };
  115. NavigationPage.SetHasBackButton(page, false);
  116. await SampleNav.PushAsync(page, null);
  117. };
  118. groups[group].Children.Add(card);
  119. }
  120. foreach (var groupName in groupOrder)
  121. {
  122. stack.Children.Add(new TextBlock
  123. {
  124. Text = groupName,
  125. FontSize = 13,
  126. FontWeight = FontWeight.SemiBold,
  127. Margin = new Avalonia.Thickness(0, 0, 0, 4),
  128. Opacity = 0.6
  129. });
  130. stack.Children.Add(groups[groupName]);
  131. }
  132. var homePage = new ContentPage
  133. {
  134. Content = new ScrollViewer { Content = stack },
  135. HorizontalContentAlignment = HorizontalAlignment.Stretch,
  136. VerticalContentAlignment = VerticalAlignment.Stretch
  137. };
  138. NavigationPage.SetHasNavigationBar(homePage, false);
  139. return homePage;
  140. }
  141. }
  142. }