DialogsPage.xaml.cs 6.6 KB

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