DialogsPage.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Avalonia.Controls;
  6. using Avalonia.Markup.Xaml;
  7. #pragma warning disable 4014
  8. namespace ControlCatalog.Pages
  9. {
  10. public class DialogsPage : UserControl
  11. {
  12. public DialogsPage()
  13. {
  14. this.InitializeComponent();
  15. List<FileDialogFilter> GetFilters()
  16. {
  17. if (this.FindControl<CheckBox>("UseFilters").IsChecked != true)
  18. return null;
  19. return new List<FileDialogFilter>
  20. {
  21. new FileDialogFilter
  22. {
  23. Name = "Text files (.txt)", Extensions = new List<string> {"txt"}
  24. },
  25. new FileDialogFilter
  26. {
  27. Name = "All files",
  28. Extensions = new List<string> {"*"}
  29. }
  30. };
  31. }
  32. this.FindControl<Button>("OpenFile").Click += delegate
  33. {
  34. new OpenFileDialog()
  35. {
  36. Title = "Open file",
  37. Filters = GetFilters(),
  38. // Almost guaranteed to exist
  39. InitialFileName = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName
  40. }.ShowAsync(GetWindow());
  41. };
  42. this.FindControl<Button>("SaveFile").Click += delegate
  43. {
  44. new SaveFileDialog()
  45. {
  46. Title = "Save file",
  47. Filters = GetFilters(),
  48. InitialFileName = "test.txt"
  49. }.ShowAsync(GetWindow());
  50. };
  51. this.FindControl<Button>("SelectFolder").Click += delegate
  52. {
  53. new OpenFolderDialog()
  54. {
  55. Title = "Select folder",
  56. }.ShowAsync(GetWindow());
  57. };
  58. this.FindControl<Button>("DecoratedWindow").Click += delegate
  59. {
  60. new DecoratedWindow().Show();
  61. };
  62. this.FindControl<Button>("DecoratedWindowDialog").Click += delegate
  63. {
  64. new DecoratedWindow().ShowDialog(GetWindow());
  65. };
  66. this.FindControl<Button>("Dialog").Click += delegate
  67. {
  68. var window = CreateSampleWindow();
  69. window.Height = 200;
  70. window.ShowDialog(GetWindow());
  71. };
  72. this.FindControl<Button>("DialogNoTaskbar").Click += delegate
  73. {
  74. var window = CreateSampleWindow();
  75. window.Height = 200;
  76. window.ShowInTaskbar = false;
  77. window.ShowDialog(GetWindow());
  78. };
  79. }
  80. private Window CreateSampleWindow()
  81. {
  82. var window = new Window();
  83. window.Height = 200;
  84. window.Width = 200;
  85. window.Content = new TextBlock { Text = "Hello world!" };
  86. window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  87. return window;
  88. }
  89. Window GetWindow() => (Window)this.VisualRoot;
  90. private void InitializeComponent()
  91. {
  92. AvaloniaXamlLoader.Load(this);
  93. }
  94. }
  95. }