123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Interactivity;
- using Avalonia.Markup.Xaml;
- using Avalonia.VisualTree;
- namespace IntegrationTestApp
- {
- public class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- InitializeViewMenu();
- this.AttachDevTools();
- AddHandler(Button.ClickEvent, OnButtonClick);
- ListBoxItems = Enumerable.Range(0, 100).Select(x => "Item " + x).ToList();
- DataContext = this;
- }
- public List<string> ListBoxItems { get; }
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- }
- private void InitializeViewMenu()
- {
- var mainTabs = this.FindControl<TabControl>("MainTabs");
- var viewMenu = (NativeMenuItem)NativeMenu.GetMenu(this).Items[1];
- foreach (TabItem tabItem in mainTabs.Items)
- {
- var menuItem = new NativeMenuItem
- {
- Header = (string)tabItem.Header!,
- IsChecked = tabItem.IsSelected,
- ToggleType = NativeMenuItemToggleType.Radio,
- };
- menuItem.Click += (s, e) => tabItem.IsSelected = true;
- viewMenu.Menu.Items.Add(menuItem);
- }
- }
- private void ShowWindow()
- {
- var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
- var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
- var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
- var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
- var owner = (Window)this.GetVisualRoot()!;
- var window = new ShowWindowTest
- {
- WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex,
- };
- if (size.HasValue)
- {
- window.Width = size.Value.Width;
- window.Height = size.Value.Height;
- }
- sizeTextBox.Text = string.Empty;
- switch (modeComboBox.SelectedIndex)
- {
- case 0:
- window.Show();
- break;
- case 1:
- window.Show(owner);
- break;
- case 2:
- window.ShowDialog(owner);
- break;
- }
- }
-
- private void MenuClicked(object? sender, RoutedEventArgs e)
- {
- var clickedMenuItemTextBlock = this.FindControl<TextBlock>("ClickedMenuItem");
- clickedMenuItemTextBlock.Text = ((MenuItem)sender!).Header.ToString();
- }
- private void OnButtonClick(object? sender, RoutedEventArgs e)
- {
- var source = e.Source as Button;
- if (source?.Name == "ComboBoxSelectionClear")
- this.FindControl<ComboBox>("BasicComboBox").SelectedIndex = -1;
- if (source?.Name == "ComboBoxSelectFirst")
- this.FindControl<ComboBox>("BasicComboBox").SelectedIndex = 0;
- if (source?.Name == "ListBoxSelectionClear")
- this.FindControl<ListBox>("BasicListBox").SelectedIndex = -1;
- if (source?.Name == "MenuClickedMenuItemReset")
- this.FindControl<TextBlock>("ClickedMenuItem").Text = "None";
- if (source?.Name == "ShowWindow")
- ShowWindow();
- }
- }
- }
|