MainWindow.axaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.ApplicationLifetimes;
  6. using Avalonia.Interactivity;
  7. using Avalonia.Markup.Xaml;
  8. using Avalonia.VisualTree;
  9. namespace IntegrationTestApp
  10. {
  11. public class MainWindow : Window
  12. {
  13. public MainWindow()
  14. {
  15. InitializeComponent();
  16. InitializeViewMenu();
  17. this.AttachDevTools();
  18. AddHandler(Button.ClickEvent, OnButtonClick);
  19. ListBoxItems = Enumerable.Range(0, 100).Select(x => "Item " + x).ToList();
  20. DataContext = this;
  21. }
  22. public List<string> ListBoxItems { get; }
  23. private void InitializeComponent()
  24. {
  25. AvaloniaXamlLoader.Load(this);
  26. }
  27. private void InitializeViewMenu()
  28. {
  29. var mainTabs = this.Get<TabControl>("MainTabs");
  30. var viewMenu = (NativeMenuItem)NativeMenu.GetMenu(this).Items[1];
  31. if (mainTabs.Items is not null)
  32. {
  33. foreach (TabItem tabItem in mainTabs.Items)
  34. {
  35. var menuItem = new NativeMenuItem
  36. {
  37. Header = (string)tabItem.Header!,
  38. IsChecked = tabItem.IsSelected,
  39. ToggleType = NativeMenuItemToggleType.Radio,
  40. };
  41. menuItem.Click += (s, e) => tabItem.IsSelected = true;
  42. viewMenu?.Menu?.Items.Add(menuItem);
  43. }
  44. }
  45. }
  46. private void ShowWindow()
  47. {
  48. var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
  49. var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
  50. var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
  51. var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
  52. var owner = (Window)this.GetVisualRoot()!;
  53. var window = new ShowWindowTest
  54. {
  55. WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex,
  56. };
  57. if (size.HasValue)
  58. {
  59. window.Width = size.Value.Width;
  60. window.Height = size.Value.Height;
  61. }
  62. sizeTextBox.Text = string.Empty;
  63. switch (modeComboBox.SelectedIndex)
  64. {
  65. case 0:
  66. window.Show();
  67. break;
  68. case 1:
  69. window.Show(owner);
  70. break;
  71. case 2:
  72. window.ShowDialog(owner);
  73. break;
  74. }
  75. }
  76. private void SendToBack()
  77. {
  78. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  79. foreach (var window in lifetime.Windows)
  80. {
  81. window.Activate();
  82. }
  83. }
  84. private void RestoreAll()
  85. {
  86. var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
  87. foreach (var window in lifetime.Windows)
  88. {
  89. window.Show();
  90. if (window.WindowState == WindowState.Minimized)
  91. window.WindowState = WindowState.Normal;
  92. }
  93. }
  94. private void MenuClicked(object? sender, RoutedEventArgs e)
  95. {
  96. var clickedMenuItemTextBlock = this.Get<TextBlock>("ClickedMenuItem");
  97. clickedMenuItemTextBlock.Text = (sender as MenuItem)?.Header?.ToString();
  98. }
  99. private void OnButtonClick(object? sender, RoutedEventArgs e)
  100. {
  101. var source = e.Source as Button;
  102. if (source?.Name == "ComboBoxSelectionClear")
  103. this.Get<ComboBox>("BasicComboBox").SelectedIndex = -1;
  104. if (source?.Name == "ComboBoxSelectFirst")
  105. this.Get<ComboBox>("BasicComboBox").SelectedIndex = 0;
  106. if (source?.Name == "ListBoxSelectionClear")
  107. this.Get<ListBox>("BasicListBox").SelectedIndex = -1;
  108. if (source?.Name == "MenuClickedMenuItemReset")
  109. this.Get<TextBlock>("ClickedMenuItem").Text = "None";
  110. if (source?.Name == "ShowWindow")
  111. ShowWindow();
  112. if (source?.Name == "SendToBack")
  113. SendToBack();
  114. if (source?.Name == "ExitFullscreen")
  115. WindowState = WindowState.Normal;
  116. if (source?.Name == "RestoreAll")
  117. RestoreAll();
  118. }
  119. }
  120. }