MainWindow.axaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia;
  5. using Avalonia.Controls;
  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.FindControl<TabControl>("MainTabs");
  30. var viewMenu = (NativeMenuItem)NativeMenu.GetMenu(this).Items[1];
  31. foreach (TabItem tabItem in mainTabs.Items)
  32. {
  33. var menuItem = new NativeMenuItem
  34. {
  35. Header = (string)tabItem.Header!,
  36. IsChecked = tabItem.IsSelected,
  37. ToggleType = NativeMenuItemToggleType.Radio,
  38. };
  39. menuItem.Click += (s, e) => tabItem.IsSelected = true;
  40. viewMenu.Menu.Items.Add(menuItem);
  41. }
  42. }
  43. private void ShowWindow()
  44. {
  45. var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
  46. var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
  47. var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
  48. var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
  49. var owner = (Window)this.GetVisualRoot()!;
  50. var window = new ShowWindowTest
  51. {
  52. WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex,
  53. };
  54. if (size.HasValue)
  55. {
  56. window.Width = size.Value.Width;
  57. window.Height = size.Value.Height;
  58. }
  59. sizeTextBox.Text = string.Empty;
  60. switch (modeComboBox.SelectedIndex)
  61. {
  62. case 0:
  63. window.Show();
  64. break;
  65. case 1:
  66. window.Show(owner);
  67. break;
  68. case 2:
  69. window.ShowDialog(owner);
  70. break;
  71. }
  72. }
  73. private void MenuClicked(object? sender, RoutedEventArgs e)
  74. {
  75. var clickedMenuItemTextBlock = this.FindControl<TextBlock>("ClickedMenuItem");
  76. clickedMenuItemTextBlock.Text = ((MenuItem)sender!).Header.ToString();
  77. }
  78. private void OnButtonClick(object? sender, RoutedEventArgs e)
  79. {
  80. var source = e.Source as Button;
  81. if (source?.Name == "ComboBoxSelectionClear")
  82. this.FindControl<ComboBox>("BasicComboBox").SelectedIndex = -1;
  83. if (source?.Name == "ComboBoxSelectFirst")
  84. this.FindControl<ComboBox>("BasicComboBox").SelectedIndex = 0;
  85. if (source?.Name == "ListBoxSelectionClear")
  86. this.FindControl<ListBox>("BasicListBox").SelectedIndex = -1;
  87. if (source?.Name == "MenuClickedMenuItemReset")
  88. this.FindControl<TextBlock>("ClickedMenuItem").Text = "None";
  89. if (source?.Name == "ShowWindow")
  90. ShowWindow();
  91. }
  92. }
  93. }