FlyoutsPage.axaml.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Controls.Primitives.PopupPositioning;
  6. using Avalonia.Interactivity;
  7. namespace ControlCatalog.Pages
  8. {
  9. public partial class FlyoutsPage : UserControl
  10. {
  11. public FlyoutsPage()
  12. {
  13. InitializeComponent();
  14. AttachedFlyoutPanel.DoubleTapped += Afp_DoubleTapped;
  15. SetXamlTexts();
  16. }
  17. private void Afp_DoubleTapped(object? sender, RoutedEventArgs e)
  18. {
  19. if (sender is Panel p)
  20. {
  21. FlyoutBase.ShowAttachedFlyout(p);
  22. }
  23. }
  24. private void SetXamlTexts()
  25. {
  26. var bfxt = ButtonFlyoutXamlText;
  27. bfxt.Text = "<Button Content=\"Click me!\">\n" +
  28. " <Button.Flyout>\n" +
  29. " <Flyout>\n" +
  30. " <Panel Width=\"100\" Height=\"100\">\n" +
  31. " <TextBlock Text=\"Flyout Content!\" />\n" +
  32. " </Panel>\n" +
  33. " </Flyout>\n" +
  34. " </Button.Flyout>\n</Button>";
  35. var mfxt = this.MenuFlyoutXamlText;
  36. mfxt.Text = "<Button Content=\"Click me!\">\n" +
  37. " <Button.Flyout>\n" +
  38. " <MenuFlyout>\n" +
  39. " <MenuItem Header=\"Item 1\">\n" +
  40. " <MenuItem Header=\"Item 2\">\n" +
  41. " </MenuFlyout>\n" +
  42. " </Button.Flyout>\n</Button>";
  43. var afxt = this.AttachedFlyoutXamlText;
  44. afxt.Text = "<Panel Name=\"AttachedFlyoutPanel\">\n" +
  45. " <FlyoutBase.AttachedFlyout>\n" +
  46. " <Flyout>\n" +
  47. " <Panel Height=\"100\">\n" +
  48. " <TextBlock Text=\"Attached Flyout\" />\n" +
  49. " </Panel>\n" +
  50. " </Flyout>\n" +
  51. " </FlyoutBase.AttachedFlyout>\n</Panel>" +
  52. "\n\n In DoubleTapped handler:\n" +
  53. "FlyoutBase.ShowAttachedFlyout(AttachedFlyoutPanel);";
  54. var sfxt = this.SharedFlyoutXamlText;
  55. sfxt.Text = "Declare a flyout in Resources:\n" +
  56. "<Window.Resources>\n" +
  57. " <Flyout x:Key=\"SharedFlyout\">\n" +
  58. " <Panel Width=\"100\" Height=\"100\">\n" +
  59. " <TextBlock Text=\"Flyout Content!\" />\n" +
  60. " </Panel>\n" +
  61. " </Flyout>\n</Window.Resources>\n\n" +
  62. "Then attach the flyout where you want it:\n" +
  63. "<Button Content=\"Launch Flyout here\" Flyout=\"{StaticResource SharedFlyout}\" />";
  64. }
  65. public void CustomPlacementCallback(CustomPopupPlacement placement)
  66. {
  67. var r = new Random().Next();
  68. placement.Anchor = (r % 4) switch
  69. {
  70. 1 => PopupAnchor.Top,
  71. 2 => PopupAnchor.Left,
  72. 3 => PopupAnchor.Right,
  73. _ => PopupAnchor.Bottom,
  74. };
  75. placement.Gravity = (r % 4) switch
  76. {
  77. 1 => PopupGravity.Top,
  78. 2 => PopupGravity.Left,
  79. 3 => PopupGravity.Right,
  80. _ => PopupGravity.Bottom,
  81. };
  82. placement.Offset = new Point(r % 20, r % 20);
  83. }
  84. }
  85. }