-
Notifications
You must be signed in to change notification settings - Fork 359
Public Spec for Microsoft.Windows.Storage.Pickers #5155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
46e650d
public spec for Microsoft.Windows.Storage.Pickers
DinahK-2SO 7e1c236
add API definition; fix errors; graceful
cb97ad7
PickFileResult and PickFolderResult
8802c66
resolve comments
7f143ef
add Windows.Storage.StorageFile SuggestedSaveFile for backward compat…
ad67028
add comment about HomeGroup may be removed in future.
a280ab8
Update specs/StoragePickers/FileSavePicker.md
DinahK-2SO 00dabd1
Update specs/StoragePickers/PickerLocationId.md
DinahK-2SO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
FileOpenPicker Class | ||
=== | ||
|
||
# Background | ||
|
||
Namespace: [Microsoft.Windows.Storage.Pickers](./Microsoft.Windows.Storage.Pickers.md) | ||
|
||
Represents a UI element that lets the user choose and open files. | ||
|
||
Supports specifying the initial location, extension filters, and text on commit button. | ||
|
||
# API Pages | ||
|
||
## Definition | ||
|
||
```C# | ||
runtimeclass FileOpenPicker | ||
{ | ||
FileOpenPicker(Microsoft.UI.WindowId windowId); | ||
|
||
string CommitButtonText; | ||
string SettingsIdentifier; | ||
IVector<string> FileTypeFilter{ get; }; | ||
PickerLocationId SuggestedStartLocation; | ||
PickerViewMode ViewMode; | ||
|
||
Windows.Foundation.IAsyncOperation<PickFileResult> PickSingleFileAsync(); | ||
Windows.Foundation.IAsyncOperation<IVectorView<PickFileResult>> PickMultipleFilesAsync(); | ||
} | ||
``` | ||
|
||
## Constructor | ||
|
||
### Examples | ||
C# | ||
|
||
```C# | ||
using Microsoft.Windows.Storage.Pickers; | ||
|
||
var openPicker = new FileOpenPicker(this.AppWindow.Id) | ||
{ | ||
// (Optional) specify the initial location. | ||
// If not specified, using PickerLocationId.Unspecified by default. | ||
SuggestedStartLocation = PickerLocationId.DocumentsLibrary, | ||
|
||
// (Optional) specify the text displayed on commit button. If not specified, use system default. | ||
CommitButtonText = "Choose selected files", | ||
|
||
// (Optional) specify file extensions filters. If not specified, default to all (*.*) | ||
FileTypeFilter = { ".txt", ".pdf", ".doc", ".docx" }, | ||
}; | ||
``` | ||
|
||
C++ | ||
|
||
```C++ | ||
#include <winrt/Microsoft.Windows.Storage.Pickers.h> | ||
using namespace winrt::Microsoft::Windows::Storage::Pickers; | ||
|
||
FileOpenPicker openPicker(AppWindow().Id()); | ||
|
||
// (Optional) specify the initial location. | ||
// If not specified, using PickerLocationId.Unspecified by default. | ||
openPicker.SuggestedStartLocation(PickerLocationId::DocumentsLibrary); | ||
|
||
// (Optional) specify the text displayed on commit button. If not specified, use system default. | ||
openPicker.CommitButtonText(L"Choose selected files"); | ||
|
||
// (Optional) specify file extensions filters. If not specified, default to all (*.*) | ||
openPicker.FileTypeFilter().Append(L".txt"); | ||
openPicker.FileTypeFilter().Append(L".pdf"); | ||
openPicker.FileTypeFilter().Append(L".doc"); | ||
openPicker.FileTypeFilter().Append(L".docx"); | ||
``` | ||
|
||
## FileOpenPicker.PickSingleFileAsync | ||
|
||
Displays a UI element that allows user to choose and open one file. | ||
|
||
Returns a light weight object that has the path of the picked file. | ||
|
||
Return null if the file dialog was cancelled or closed without selection. | ||
|
||
### Examples | ||
|
||
C# | ||
|
||
```C# | ||
using Microsoft.Windows.Storage.Pickers; | ||
|
||
var openPicker = new FileOpenPicker(this.AppWindow.Id); | ||
|
||
var result = await openPicker.PickSingleFileAsync(); | ||
if (result is not null) | ||
{ | ||
var content = System.IO.File.ReadAllText(result.Path); | ||
} | ||
else | ||
{ | ||
// error handling. | ||
} | ||
``` | ||
|
||
C++ | ||
```C++ | ||
#include <winrt/Microsoft.Windows.Storage.Pickers.h> | ||
#include <fstream> | ||
#include <string> | ||
using namespace winrt::Microsoft::Windows::Storage::Pickers; | ||
|
||
FileOpenPicker openPicker(AppWindow().Id()); | ||
auto& result{ co_await openPicker.PickSingleFileAsync() }; | ||
if (result) | ||
{ | ||
std::ifstream fileReader(result.Path().c_str()); | ||
std::string text((std::istreambuf_iterator<char>(fileReader)), std::istreambuf_iterator<char>()); | ||
winrt::hstring hText = winrt::to_hstring(text); | ||
} | ||
else | ||
{ | ||
// error handling. | ||
} | ||
``` | ||
|
||
## FileOpenPicker.PickMultipleFilesAsync | ||
|
||
Displays a UI element that allows user to choose and open multiple files. | ||
|
||
Returns a collection of light weight objects that has the path of picked files. | ||
|
||
Return an empty list (Count = 0) if the file dialog's cancelled or closed without selection. | ||
|
||
### Examples | ||
|
||
C# | ||
|
||
```C# | ||
using Microsoft.Windows.Storage.Pickers; | ||
|
||
var openPicker = new FileOpenPicker(this.AppWindow.Id); | ||
|
||
var results = await openPicker.PickMultipleFilesAsync(); | ||
if (results.Count > 0) | ||
{ | ||
var pickedFilePaths = results.Select(f => f.Path); | ||
foreach (var path in pickedFilePaths) | ||
{ | ||
var content = System.IO.File.ReadAllText(path); | ||
} | ||
} | ||
else | ||
{ | ||
// error handling. | ||
} | ||
``` | ||
|
||
C++ | ||
```C++ | ||
#include <winrt/Microsoft.Windows.Storage.Pickers.h> | ||
#include <fstream> | ||
#include <string> | ||
using namespace winrt::Microsoft::Windows::Storage::Pickers; | ||
|
||
FileOpenPicker openPicker(AppWindow().Id()); | ||
auto& results{ co_await openPicker.PickMultipleFilesAsync() }; | ||
if (results.Size() > 0) | ||
{ | ||
for (auto const& result : results) | ||
{ | ||
std::ifstream fileReader(result.Path().c_str()); | ||
std::string text((std::istreambuf_iterator<char>(fileReader)), std::istreambuf_iterator<char>()); | ||
winrt::hstring hText = winrt::to_hstring(text); | ||
} | ||
} | ||
else | ||
{ | ||
// error handling. | ||
} | ||
``` | ||
|
||
# See Also | ||
|
||
[PickFileResult](./PickFileResult.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
FileSavePicker Class | ||
=== | ||
|
||
# Background | ||
|
||
Namespace: [Microsoft.Windows.Storage.Pickers](./Microsoft.Windows.Storage.Pickers.md) | ||
|
||
Represents a UI element that lets the user choose a file to save. | ||
|
||
# API Pages | ||
|
||
## Definition | ||
|
||
```C# | ||
runtimeclass FileSavePicker | ||
{ | ||
FileSavePicker(Microsoft.UI.WindowId windowId); | ||
|
||
string CommitButtonText; | ||
|
||
string SettingsIdentifier; | ||
string DefaultFileExtension; | ||
string SuggestedFileName; | ||
Windows.Storage.StorageFile SuggestedSaveFile; | ||
IMap<string, IVector<string>> FileTypeChoices{ get; }; | ||
|
||
PickerLocationId SuggestedStartLocation; | ||
|
||
Windows.Foundation.IAsyncOperation<PickFileResult> PickSaveFileAsync() | ||
} | ||
``` | ||
|
||
## Constructor | ||
|
||
### Examples | ||
C# | ||
|
||
```C# | ||
using Microsoft.Windows.Storage.Pickers; | ||
|
||
var savePicker = new FileSavePicker(this.AppWindow.Id) | ||
{ | ||
// (Optional) specify the initial location. | ||
// If not specified, using PickerLocationId.Unspecified by default. | ||
SuggestedStartLocation = PickerLocationId.DocumentsLibrary, | ||
|
||
// (Optional) specify the default file name. If not specified, use system default. | ||
SuggestedFileName = "My Document", | ||
|
||
// (Optional) specify the text displayed on commit button. If not specified, use system default. | ||
CommitButtonText = "Save Document", | ||
|
||
// (Optional) categorized extensions types. If not specified, use system default: All Files (*.*) | ||
FileTypeChoices = { | ||
{ "Documents", new List<string> { ".txt", ".doc", ".docx" } } | ||
}, | ||
|
||
// (Optional) specify the default file extension (will be appended after the default file name). | ||
// If not specified, will not appended after the default extension. | ||
DefaultFileExtension = ".txt", | ||
}; | ||
``` | ||
|
||
C++ | ||
|
||
```C++ | ||
#include <winrt/Microsoft.Windows.Storage.Pickers.h> | ||
using namespace winrt::Microsoft::Windows::Storage::Pickers; | ||
|
||
FileSavePicker savePicker(AppWindow().Id()); | ||
|
||
// (Optional) specify the initial location. | ||
// If not specified, using PickerLocationId.Unspecified by defaultl | ||
DinahK-2SO marked this conversation as resolved.
Show resolved
Hide resolved
|
||
savePicker.SuggestedStartLocation(PickerLocationId::DocumentsLibrary); | ||
|
||
// (Optional) specify the default file name. If not specified, use system default. | ||
savePicker.SuggestedFileName(L"NewDocument"); | ||
|
||
// (Optional) categorized extensions types. If not specified, use system default: All Files (*.*) | ||
savePicker.FileTypeChoices().Insert(L"Text", winrt::single_threaded_vector<winrt::hstring>({ L".txt" })); | ||
|
||
// (Optional) specify the default file extension (will be appended after the default file name). | ||
// If not specified, will not appended after the default extension. | ||
savePicker.DefaultFileExtension(L".txt"); | ||
``` | ||
|
||
## FileSavePicker.PickSaveFileAsync | ||
|
||
Displays a UI element that allows the user to configure the file path to save. | ||
|
||
Returns a light weight object that has the path of the saved file. | ||
|
||
Returns null if the file dialog was cancelled or closed without saved file. | ||
|
||
### Examples | ||
|
||
C# | ||
|
||
```C# | ||
using Microsoft.Windows.Storage.Pickers; | ||
|
||
var savePicker = new FileSavePicker(this.AppWindow.Id); | ||
var result = await savePicker.PickSaveFileAsync(); | ||
if (result is not null) | ||
{ | ||
System.IO.File.WriteAllText(result.Path, "Hello world."); | ||
} | ||
else | ||
{ | ||
// error handling. | ||
} | ||
``` | ||
|
||
C++ | ||
|
||
```C++ | ||
#include <winrt/Microsoft.Windows.Storage.Pickers.h> | ||
#include <fstream> | ||
#include <string> | ||
using namespace winrt::Microsoft::Windows::Storage::Pickers; | ||
|
||
FileSavePicker savePicker(AppWindow().Id()); | ||
auto& result{ co_await savePicker.PickSaveFileAsync() }; | ||
if (result) | ||
{ | ||
std::ofstream outFile(result.Path().c_str()); | ||
outFile << "Hello world."; | ||
outFile.close(); | ||
} | ||
else | ||
{ | ||
// error handling. | ||
} | ||
``` | ||
|
||
# See Also | ||
|
||
[PickFileResult](./PickFileResult.md) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.