Browse Source

Reinstated DialogsPage.

pull/9677/head
Steven Kirk 4 years ago
parent
commit
84abb69b61
  1. 2
      samples/ControlCatalog/Pages/DialogsPage.xaml
  2. 590
      samples/ControlCatalog/Pages/DialogsPage.xaml.cs

2
samples/ControlCatalog/Pages/DialogsPage.xaml

@ -46,7 +46,7 @@
Classes="h2" Classes="h2"
IsVisible="False" IsVisible="False"
Text="Last picker results:" /> Text="Last picker results:" />
<ItemsPresenter x:Name="PickerLastResults" /> <ItemsControl x:Name="PickerLastResults" />
<TextBox Name="BookmarkContainer" Watermark="Bookmark" /> <TextBox Name="BookmarkContainer" Watermark="Bookmark" />
<TextBox Name="OpenedFileContent" <TextBox Name="OpenedFileContent"

590
samples/ControlCatalog/Pages/DialogsPage.xaml.cs

@ -24,301 +24,301 @@ namespace ControlCatalog.Pages
{ {
this.InitializeComponent(); this.InitializeComponent();
//// var results = this.Get<ItemsPresenter>("PickerLastResults"); var results = this.Get<ItemsControl>("PickerLastResults");
//// var resultsVisible = this.Get<TextBlock>("PickerLastResultsVisible"); var resultsVisible = this.Get<TextBlock>("PickerLastResultsVisible");
//// var bookmarkContainer = this.Get<TextBox>("BookmarkContainer"); var bookmarkContainer = this.Get<TextBox>("BookmarkContainer");
//// var openedFileContent = this.Get<TextBox>("OpenedFileContent"); var openedFileContent = this.Get<TextBox>("OpenedFileContent");
//// var openMultiple = this.Get<CheckBox>("OpenMultiple"); var openMultiple = this.Get<CheckBox>("OpenMultiple");
//// IStorageFolder? lastSelectedDirectory = null; IStorageFolder? lastSelectedDirectory = null;
//// List<FileDialogFilter> GetFilters() List<FileDialogFilter> GetFilters()
//// { {
//// if (this.Get<CheckBox>("UseFilters").IsChecked != true) if (this.Get<CheckBox>("UseFilters").IsChecked != true)
//// return new List<FileDialogFilter>(); return new List<FileDialogFilter>();
//// return new List<FileDialogFilter> return new List<FileDialogFilter>
//// { {
//// new FileDialogFilter new FileDialogFilter
//// { {
//// Name = "Text files (.txt)", Extensions = new List<string> {"txt"} Name = "Text files (.txt)", Extensions = new List<string> {"txt"}
//// }, },
//// new FileDialogFilter new FileDialogFilter
//// { {
//// Name = "All files", Name = "All files",
//// Extensions = new List<string> {"*"} Extensions = new List<string> {"*"}
//// } }
//// }; };
//// } }
//// List<FilePickerFileType>? GetFileTypes() List<FilePickerFileType>? GetFileTypes()
//// { {
//// if (this.Get<CheckBox>("UseFilters").IsChecked != true) if (this.Get<CheckBox>("UseFilters").IsChecked != true)
//// return null; return null;
//// return new List<FilePickerFileType> return new List<FilePickerFileType>
//// { {
//// FilePickerFileTypes.All, FilePickerFileTypes.All,
//// FilePickerFileTypes.TextPlain FilePickerFileTypes.TextPlain
//// }; };
//// } }
//// this.Get<Button>("OpenFile").Click += async delegate this.Get<Button>("OpenFile").Click += async delegate
//// { {
//// // Almost guaranteed to exist // Almost guaranteed to exist
//// var uri = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName; var uri = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName;
//// var initialFileName = uri == null ? null : System.IO.Path.GetFileName(uri); var initialFileName = uri == null ? null : System.IO.Path.GetFileName(uri);
//// var initialDirectory = uri == null ? null : System.IO.Path.GetDirectoryName(uri); var initialDirectory = uri == null ? null : System.IO.Path.GetDirectoryName(uri);
//// var result = await new OpenFileDialog() var result = await new OpenFileDialog()
//// { {
//// Title = "Open file", Title = "Open file",
//// Filters = GetFilters(), Filters = GetFilters(),
//// Directory = initialDirectory, Directory = initialDirectory,
//// InitialFileName = initialFileName InitialFileName = initialFileName
//// }.ShowAsync(GetWindow()); }.ShowAsync(GetWindow());
//// results.Items = result; results.Items = result;
//// resultsVisible.IsVisible = result?.Any() == true; resultsVisible.IsVisible = result?.Any() == true;
//// }; };
//// this.Get<Button>("OpenMultipleFiles").Click += async delegate this.Get<Button>("OpenMultipleFiles").Click += async delegate
//// { {
//// var result = await new OpenFileDialog() var result = await new OpenFileDialog()
//// { {
//// Title = "Open multiple files", Title = "Open multiple files",
//// Filters = GetFilters(), Filters = GetFilters(),
//// Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null, Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null,
//// AllowMultiple = true AllowMultiple = true
//// }.ShowAsync(GetWindow()); }.ShowAsync(GetWindow());
//// results.Items = result; results.Items = result;
//// resultsVisible.IsVisible = result?.Any() == true; resultsVisible.IsVisible = result?.Any() == true;
//// }; };
//// this.Get<Button>("SaveFile").Click += async delegate this.Get<Button>("SaveFile").Click += async delegate
//// { {
//// var filters = GetFilters(); var filters = GetFilters();
//// var result = await new SaveFileDialog() var result = await new SaveFileDialog()
//// { {
//// Title = "Save file", Title = "Save file",
//// Filters = filters, Filters = filters,
//// Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null, Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null,
//// DefaultExtension = filters?.Any() == true ? "txt" : null, DefaultExtension = filters?.Any() == true ? "txt" : null,
//// InitialFileName = "test.txt" InitialFileName = "test.txt"
//// }.ShowAsync(GetWindow()); }.ShowAsync(GetWindow());
//// results.Items = new[] { result }; results.Items = new[] { result };
//// resultsVisible.IsVisible = result != null; resultsVisible.IsVisible = result != null;
//// }; };
//// this.Get<Button>("SelectFolder").Click += async delegate this.Get<Button>("SelectFolder").Click += async delegate
//// { {
//// var result = await new OpenFolderDialog() var result = await new OpenFolderDialog()
//// { {
//// Title = "Select folder", Title = "Select folder",
//// Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null
//// }.ShowAsync(GetWindow()); }.ShowAsync(GetWindow());
//// if (string.IsNullOrEmpty(result)) if (string.IsNullOrEmpty(result))
//// { {
//// resultsVisible.IsVisible = false; resultsVisible.IsVisible = false;
//// } }
//// else else
//// { {
//// lastSelectedDirectory = new BclStorageFolder(new System.IO.DirectoryInfo(result)); lastSelectedDirectory = new BclStorageFolder(new System.IO.DirectoryInfo(result));
//// results.Items = new[] { result }; results.Items = new[] { result };
//// resultsVisible.IsVisible = true; resultsVisible.IsVisible = true;
//// } }
//// }; };
//// this.Get<Button>("OpenBoth").Click += async delegate this.Get<Button>("OpenBoth").Click += async delegate
//// { {
//// var result = await new OpenFileDialog() var result = await new OpenFileDialog()
//// { {
//// Title = "Select both", Title = "Select both",
//// Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null, Directory = lastSelectedDirectory?.TryGetUri(out var path) == true ? path.LocalPath : null,
//// AllowMultiple = true AllowMultiple = true
//// }.ShowManagedAsync(GetWindow(), new ManagedFileDialogOptions }.ShowManagedAsync(GetWindow(), new ManagedFileDialogOptions
//// { {
//// AllowDirectorySelection = true AllowDirectorySelection = true
//// }); });
//// results.Items = result; results.Items = result;
//// resultsVisible.IsVisible = result?.Any() == true; resultsVisible.IsVisible = result?.Any() == true;
//// }; };
//// this.Get<Button>("DecoratedWindow").Click += delegate this.Get<Button>("DecoratedWindow").Click += delegate
//// { {
//// new DecoratedWindow().Show(); new DecoratedWindow().Show();
//// }; };
//// this.Get<Button>("DecoratedWindowDialog").Click += delegate this.Get<Button>("DecoratedWindowDialog").Click += delegate
//// { {
//// _ = new DecoratedWindow().ShowDialog(GetWindow()); _ = new DecoratedWindow().ShowDialog(GetWindow());
//// }; };
//// this.Get<Button>("Dialog").Click += delegate this.Get<Button>("Dialog").Click += delegate
//// { {
//// var window = CreateSampleWindow(); var window = CreateSampleWindow();
//// window.Height = 200; window.Height = 200;
//// _ = window.ShowDialog(GetWindow()); _ = window.ShowDialog(GetWindow());
//// }; };
//// this.Get<Button>("DialogNoTaskbar").Click += delegate this.Get<Button>("DialogNoTaskbar").Click += delegate
//// { {
//// var window = CreateSampleWindow(); var window = CreateSampleWindow();
//// window.Height = 200; window.Height = 200;
//// window.ShowInTaskbar = false; window.ShowInTaskbar = false;
//// _ = window.ShowDialog(GetWindow()); _ = window.ShowDialog(GetWindow());
//// }; };
//// this.Get<Button>("OwnedWindow").Click += delegate this.Get<Button>("OwnedWindow").Click += delegate
//// { {
//// var window = CreateSampleWindow(); var window = CreateSampleWindow();
//// window.Show(GetWindow()); window.Show(GetWindow());
//// }; };
//// this.Get<Button>("OwnedWindowNoTaskbar").Click += delegate this.Get<Button>("OwnedWindowNoTaskbar").Click += delegate
//// { {
//// var window = CreateSampleWindow(); var window = CreateSampleWindow();
//// window.ShowInTaskbar = false; window.ShowInTaskbar = false;
//// window.Show(GetWindow()); window.Show(GetWindow());
//// }; };
//// this.Get<Button>("OpenFilePicker").Click += async delegate this.Get<Button>("OpenFilePicker").Click += async delegate
//// { {
//// var result = await GetStorageProvider().OpenFilePickerAsync(new FilePickerOpenOptions() var result = await GetStorageProvider().OpenFilePickerAsync(new FilePickerOpenOptions()
//// { {
//// Title = "Open file", Title = "Open file",
//// FileTypeFilter = GetFileTypes(), FileTypeFilter = GetFileTypes(),
//// SuggestedStartLocation = lastSelectedDirectory, SuggestedStartLocation = lastSelectedDirectory,
//// AllowMultiple = openMultiple.IsChecked == true AllowMultiple = openMultiple.IsChecked == true
//// }); });
//// await SetPickerResult(result); await SetPickerResult(result);
//// }; };
//// this.Get<Button>("SaveFilePicker").Click += async delegate this.Get<Button>("SaveFilePicker").Click += async delegate
//// { {
//// var fileTypes = GetFileTypes(); var fileTypes = GetFileTypes();
//// var file = await GetStorageProvider().SaveFilePickerAsync(new FilePickerSaveOptions() var file = await GetStorageProvider().SaveFilePickerAsync(new FilePickerSaveOptions()
//// { {
//// Title = "Save file", Title = "Save file",
//// FileTypeChoices = fileTypes, FileTypeChoices = fileTypes,
//// SuggestedStartLocation = lastSelectedDirectory, SuggestedStartLocation = lastSelectedDirectory,
//// SuggestedFileName = "FileName", SuggestedFileName = "FileName",
//// DefaultExtension = fileTypes?.Any() == true ? "txt" : null, DefaultExtension = fileTypes?.Any() == true ? "txt" : null,
//// ShowOverwritePrompt = false ShowOverwritePrompt = false
//// }); });
//// if (file is not null && file.CanOpenWrite) if (file is not null && file.CanOpenWrite)
//// { {
//// // Sync disposal of StreamWriter is not supported on WASM // Sync disposal of StreamWriter is not supported on WASM
////#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
//// await using var stream = await file.OpenWriteAsync(); await using var stream = await file.OpenWriteAsync();
//// await using var reader = new System.IO.StreamWriter(stream); await using var reader = new System.IO.StreamWriter(stream);
////#else #else
//// using var stream = await file.OpenWriteAsync(); using var stream = await file.OpenWriteAsync();
//// using var reader = new System.IO.StreamWriter(stream); using var reader = new System.IO.StreamWriter(stream);
////#endif #endif
//// await reader.WriteLineAsync(openedFileContent.Text); await reader.WriteLineAsync(openedFileContent.Text);
//// lastSelectedDirectory = await file.GetParentAsync(); lastSelectedDirectory = await file.GetParentAsync();
//// } }
//// await SetPickerResult(file is null ? null : new [] {file}); await SetPickerResult(file is null ? null : new[] { file });
//// }; };
//// this.Get<Button>("OpenFolderPicker").Click += async delegate this.Get<Button>("OpenFolderPicker").Click += async delegate
//// { {
//// var folders = await GetStorageProvider().OpenFolderPickerAsync(new FolderPickerOpenOptions() var folders = await GetStorageProvider().OpenFolderPickerAsync(new FolderPickerOpenOptions()
//// { {
//// Title = "Folder file", Title = "Folder file",
//// SuggestedStartLocation = lastSelectedDirectory, SuggestedStartLocation = lastSelectedDirectory,
//// AllowMultiple = openMultiple.IsChecked == true AllowMultiple = openMultiple.IsChecked == true
//// }); });
//// await SetPickerResult(folders); await SetPickerResult(folders);
//// lastSelectedDirectory = folders.FirstOrDefault(); lastSelectedDirectory = folders.FirstOrDefault();
//// }; };
//// this.Get<Button>("OpenFileFromBookmark").Click += async delegate this.Get<Button>("OpenFileFromBookmark").Click += async delegate
//// { {
//// var file = bookmarkContainer.Text is not null var file = bookmarkContainer.Text is not null
//// ? await GetStorageProvider().OpenFileBookmarkAsync(bookmarkContainer.Text) ? await GetStorageProvider().OpenFileBookmarkAsync(bookmarkContainer.Text)
//// : null; : null;
//// await SetPickerResult(file is null ? null : new[] { file }); await SetPickerResult(file is null ? null : new[] { file });
//// }; };
//// this.Get<Button>("OpenFolderFromBookmark").Click += async delegate this.Get<Button>("OpenFolderFromBookmark").Click += async delegate
//// { {
//// var folder = bookmarkContainer.Text is not null var folder = bookmarkContainer.Text is not null
//// ? await GetStorageProvider().OpenFolderBookmarkAsync(bookmarkContainer.Text) ? await GetStorageProvider().OpenFolderBookmarkAsync(bookmarkContainer.Text)
//// : null; : null;
//// await SetPickerResult(folder is null ? null : new[] { folder }); await SetPickerResult(folder is null ? null : new[] { folder });
//// lastSelectedDirectory = folder; lastSelectedDirectory = folder;
//// }; };
//// async Task SetPickerResult(IReadOnlyCollection<IStorageItem>? items) async Task SetPickerResult(IReadOnlyCollection<IStorageItem>? items)
//// { {
//// items ??= Array.Empty<IStorageItem>(); items ??= Array.Empty<IStorageItem>();
//// bookmarkContainer.Text = items.FirstOrDefault(f => f.CanBookmark) is { } f ? await f.SaveBookmarkAsync() : "Can't bookmark"; bookmarkContainer.Text = items.FirstOrDefault(f => f.CanBookmark) is { } f ? await f.SaveBookmarkAsync() : "Can't bookmark";
//// var mappedResults = new List<string>(); var mappedResults = new List<string>();
//// if (items.FirstOrDefault() is IStorageItem item) if (items.FirstOrDefault() is IStorageItem item)
//// { {
//// var resultText = item is IStorageFile ? "File:" : "Folder:"; var resultText = item is IStorageFile ? "File:" : "Folder:";
//// resultText += Environment.NewLine; resultText += Environment.NewLine;
//// var props = await item.GetBasicPropertiesAsync(); var props = await item.GetBasicPropertiesAsync();
//// resultText += @$"Size: {props.Size} resultText += @$"Size: {props.Size}
////DateCreated: {props.DateCreated} DateCreated: {props.DateCreated}
////DateModified: {props.DateModified} DateModified: {props.DateModified}
////CanBookmark: {item.CanBookmark} CanBookmark: {item.CanBookmark}
////"; ";
//// if (item is IStorageFile file) if (item is IStorageFile file)
//// { {
//// resultText += @$" resultText += @$"
////CanOpenRead: {file.CanOpenRead} CanOpenRead: {file.CanOpenRead}
////CanOpenWrite: {file.CanOpenWrite} CanOpenWrite: {file.CanOpenWrite}
////Content: Content:
////"; ";
//// if (file.CanOpenRead) if (file.CanOpenRead)
//// { {
////#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
//// await using var stream = await file.OpenReadAsync(); await using var stream = await file.OpenReadAsync();
////#else #else
//// using var stream = await file.OpenReadAsync(); using var stream = await file.OpenReadAsync();
////#endif #endif
//// using var reader = new System.IO.StreamReader(stream); using var reader = new System.IO.StreamReader(stream);
//// // 4GB file test, shouldn't load more than 10000 chars into a memory. // 4GB file test, shouldn't load more than 10000 chars into a memory.
//// const int length = 10000; const int length = 10000;
//// var buffer = ArrayPool<char>.Shared.Rent(length); var buffer = ArrayPool<char>.Shared.Rent(length);
//// try try
//// { {
//// var charsRead = await reader.ReadAsync(buffer, 0, length); var charsRead = await reader.ReadAsync(buffer, 0, length);
//// resultText += new string(buffer, 0, charsRead); resultText += new string(buffer, 0, charsRead);
//// } }
//// finally finally
//// { {
//// ArrayPool<char>.Shared.Return(buffer); ArrayPool<char>.Shared.Return(buffer);
//// } }
//// } }
//// } }
//// openedFileContent.Text = resultText; openedFileContent.Text = resultText;
//// lastSelectedDirectory = await item.GetParentAsync(); lastSelectedDirectory = await item.GetParentAsync();
//// if (lastSelectedDirectory is not null) if (lastSelectedDirectory is not null)
//// { {
//// mappedResults.Add(FullPathOrName(lastSelectedDirectory)); mappedResults.Add(FullPathOrName(lastSelectedDirectory));
//// } }
//// foreach (var selectedItem in items) foreach (var selectedItem in items)
//// { {
//// mappedResults.Add("+> " + FullPathOrName(selectedItem)); mappedResults.Add("+> " + FullPathOrName(selectedItem));
//// if (selectedItem is IStorageFolder folder) if (selectedItem is IStorageFolder folder)
//// { {
//// foreach (var innerItems in await folder.GetItemsAsync()) foreach (var innerItems in await folder.GetItemsAsync())
//// { {
//// mappedResults.Add("++> " + FullPathOrName(innerItems)); mappedResults.Add("++> " + FullPathOrName(innerItems));
//// } }
//// } }
//// } }
//// } }
//// results.Items = mappedResults; results.Items = mappedResults;
//// resultsVisible.IsVisible = mappedResults.Any(); resultsVisible.IsVisible = mappedResults.Any();
//// } }
} }
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

Loading…
Cancel
Save