DialogsPage.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Avalonia.Controls;
  6. using Avalonia.Dialogs;
  7. using Avalonia.Layout;
  8. using Avalonia.Markup.Xaml;
  9. #pragma warning disable 4014
  10. namespace ControlCatalog.Pages
  11. {
  12. public class DialogsPage : UserControl
  13. {
  14. public DialogsPage()
  15. {
  16. this.InitializeComponent();
  17. List<FileDialogFilter> GetFilters()
  18. {
  19. if (this.FindControl<CheckBox>("UseFilters").IsChecked != true)
  20. return null;
  21. return new List<FileDialogFilter>
  22. {
  23. new FileDialogFilter
  24. {
  25. Name = "Text files (.txt)", Extensions = new List<string> {"txt"}
  26. },
  27. new FileDialogFilter
  28. {
  29. Name = "All files",
  30. Extensions = new List<string> {"*"}
  31. }
  32. };
  33. }
  34. this.FindControl<Button>("OpenFile").Click += delegate
  35. {
  36. new OpenFileDialog()
  37. {
  38. Title = "Open file",
  39. Filters = GetFilters(),
  40. // Almost guaranteed to exist
  41. InitialFileName = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName
  42. }.ShowAsync(GetWindow());
  43. };
  44. this.FindControl<Button>("SaveFile").Click += delegate
  45. {
  46. new SaveFileDialog()
  47. {
  48. Title = "Save file",
  49. Filters = GetFilters(),
  50. InitialFileName = "test.txt"
  51. }.ShowAsync(GetWindow());
  52. };
  53. this.FindControl<Button>("SelectFolder").Click += delegate
  54. {
  55. new OpenFolderDialog()
  56. {
  57. Title = "Select folder",
  58. }.ShowAsync(GetWindow());
  59. };
  60. this.FindControl<Button>("OpenBoth").Click += async delegate
  61. {
  62. var res = await new OpenFileDialog()
  63. {
  64. Title = "Select both",
  65. AllowMultiple = true
  66. }.ShowManagedAsync(GetWindow(), new ManagedFileDialogOptions
  67. {
  68. AllowDirectorySelection = true
  69. });
  70. if (res != null)
  71. Console.WriteLine("Selected: \n" + string.Join("\n", res));
  72. };
  73. this.FindControl<Button>("DecoratedWindow").Click += delegate
  74. {
  75. new DecoratedWindow().Show();
  76. };
  77. this.FindControl<Button>("DecoratedWindowDialog").Click += delegate
  78. {
  79. new DecoratedWindow().ShowDialog(GetWindow());
  80. };
  81. this.FindControl<Button>("Dialog").Click += delegate
  82. {
  83. var window = CreateSampleWindow();
  84. window.Height = 200;
  85. window.ShowDialog(GetWindow());
  86. };
  87. this.FindControl<Button>("DialogNoTaskbar").Click += delegate
  88. {
  89. var window = CreateSampleWindow();
  90. window.Height = 200;
  91. window.ShowInTaskbar = false;
  92. window.ShowDialog(GetWindow());
  93. };
  94. this.FindControl<Button>("OwnedWindow").Click += delegate
  95. {
  96. var window = CreateSampleWindow();
  97. window.Show(GetWindow());
  98. };
  99. this.FindControl<Button>("OwnedWindowNoTaskbar").Click += delegate
  100. {
  101. var window = CreateSampleWindow();
  102. window.ShowInTaskbar = false;
  103. window.Show(GetWindow());
  104. };
  105. }
  106. private Window CreateSampleWindow()
  107. {
  108. Button button;
  109. var window = new Window
  110. {
  111. Height = 200,
  112. Width = 200,
  113. Content = new StackPanel
  114. {
  115. Spacing = 4,
  116. Children =
  117. {
  118. new TextBlock { Text = "Hello world!" },
  119. (button = new Button
  120. {
  121. HorizontalAlignment = HorizontalAlignment.Center,
  122. Content = "Click to close"
  123. })
  124. }
  125. },
  126. WindowStartupLocation = WindowStartupLocation.CenterOwner
  127. };
  128. button.Click += (_, __) => window.Close();
  129. return window;
  130. }
  131. Window GetWindow() => (Window)this.VisualRoot;
  132. private void InitializeComponent()
  133. {
  134. AvaloniaXamlLoader.Load(this);
  135. }
  136. }
  137. }