MainView.xaml.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Markup.Xaml.MarkupExtensions;
  7. using Avalonia.Markup.Xaml.Styling;
  8. using Avalonia.Markup.Xaml.XamlIl;
  9. using Avalonia.Platform;
  10. using ControlCatalog.Pages;
  11. namespace ControlCatalog
  12. {
  13. public class MainView : UserControl
  14. {
  15. public MainView()
  16. {
  17. AvaloniaXamlLoader.Load(this);
  18. if (AvaloniaLocator.Current.GetService<IRuntimePlatform>().GetRuntimeInfo().IsDesktop)
  19. {
  20. IList tabItems = ((IList)this.FindControl<TabControl>("Sidebar").Items);
  21. tabItems.Add(new TabItem()
  22. {
  23. Header = "Dialogs",
  24. Content = new DialogsPage()
  25. });
  26. tabItems.Add(new TabItem()
  27. {
  28. Header = "Screens",
  29. Content = new ScreenPage()
  30. });
  31. }
  32. var light = new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog"))
  33. {
  34. Source = new Uri("resm:Avalonia.Themes.Default.Accents.BaseLight.xaml?assembly=Avalonia.Themes.Default")
  35. };
  36. var dark = new StyleInclude(new Uri("resm:Styles?assembly=ControlCatalog"))
  37. {
  38. Source = new Uri("resm:Avalonia.Themes.Default.Accents.BaseDark.xaml?assembly=Avalonia.Themes.Default")
  39. };
  40. var themes = this.Find<ComboBox>("Themes");
  41. themes.SelectionChanged += (sender, e) =>
  42. {
  43. switch (themes.SelectedIndex)
  44. {
  45. case 0:
  46. Styles[0] = light;
  47. break;
  48. case 1:
  49. Styles[0] = dark;
  50. break;
  51. }
  52. };
  53. Styles.Add(light);
  54. var decorations = this.Find<ComboBox>("Decorations");
  55. decorations.SelectionChanged += (sender, e) =>
  56. {
  57. if (VisualRoot is Window window)
  58. window.SystemDecorations = (SystemDecorations)decorations.SelectedIndex;
  59. };
  60. }
  61. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  62. {
  63. base.OnAttachedToVisualTree(e);
  64. var decorations = this.Find<ComboBox>("Decorations");
  65. if (VisualRoot is Window window)
  66. decorations.SelectedIndex = (int)window.SystemDecorations;
  67. }
  68. }
  69. }