DialogsPage.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Markup.Xaml;
  8. #pragma warning disable 4014
  9. namespace ControlCatalog.Pages
  10. {
  11. public class DialogsPage : UserControl
  12. {
  13. public DialogsPage()
  14. {
  15. this.InitializeComponent();
  16. List<FileDialogFilter> GetFilters()
  17. {
  18. if (this.FindControl<CheckBox>("UseFilters").IsChecked != true)
  19. return null;
  20. return new List<FileDialogFilter>
  21. {
  22. new FileDialogFilter
  23. {
  24. Name = "Text files (.txt)", Extensions = new List<string> {"txt"}
  25. },
  26. new FileDialogFilter
  27. {
  28. Name = "All files",
  29. Extensions = new List<string> {"*"}
  30. }
  31. };
  32. }
  33. this.FindControl<Button>("OpenFile").Click += delegate
  34. {
  35. new OpenFileDialog()
  36. {
  37. Title = "Open file",
  38. Filters = GetFilters(),
  39. // Almost guaranteed to exist
  40. InitialFileName = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName
  41. }.ShowAsync(GetWindow());
  42. };
  43. this.FindControl<Button>("SaveFile").Click += delegate
  44. {
  45. new SaveFileDialog()
  46. {
  47. Title = "Save file",
  48. Filters = GetFilters(),
  49. InitialFileName = "test.txt"
  50. }.ShowAsync(GetWindow());
  51. };
  52. this.FindControl<Button>("SelectFolder").Click += delegate
  53. {
  54. new OpenFolderDialog()
  55. {
  56. Title = "Select folder",
  57. }.ShowAsync(GetWindow());
  58. };
  59. this.FindControl<Button>("OpenBoth").Click += async delegate
  60. {
  61. var res = await new OpenFileDialog()
  62. {
  63. Title = "Select both",
  64. AllowMultiple = true
  65. }.ShowManagedAsync(GetWindow(), new ManagedFileDialogOptions
  66. {
  67. AllowDirectorySelection = true
  68. });
  69. if (res != null)
  70. Console.WriteLine("Selected: \n" + string.Join("\n", res));
  71. };
  72. this.FindControl<Button>("DecoratedWindow").Click += delegate
  73. {
  74. new DecoratedWindow().Show();
  75. };
  76. this.FindControl<Button>("DecoratedWindowDialog").Click += delegate
  77. {
  78. new DecoratedWindow().ShowDialog(GetWindow());
  79. };
  80. this.FindControl<Button>("Dialog").Click += delegate
  81. {
  82. var window = CreateSampleWindow();
  83. window.Height = 200;
  84. window.ShowDialog(GetWindow());
  85. };
  86. this.FindControl<Button>("DialogNoTaskbar").Click += delegate
  87. {
  88. var window = CreateSampleWindow();
  89. window.Height = 200;
  90. window.ShowInTaskbar = false;
  91. window.ShowDialog(GetWindow());
  92. };
  93. }
  94. private Window CreateSampleWindow()
  95. {
  96. var window = new Window();
  97. window.Height = 200;
  98. window.Width = 200;
  99. window.Content = new TextBlock { Text = "Hello world!" };
  100. window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
  101. return window;
  102. }
  103. Window GetWindow() => (Window)this.VisualRoot;
  104. private void InitializeComponent()
  105. {
  106. AvaloniaXamlLoader.Load(this);
  107. }
  108. }
  109. }