MainWindow.axaml.cs 4.5 KB

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