FileOpenPicker Class
表示允许用户选择和打开文件的 UI 元素。
语法
特性
- ActivatableAttribute(NTDDI_WIN8)
- MuseAttribute()
- VersionAttribute(NTDDI_WIN8)
成员
FileOpenPicker类 具有以下类型的成员:
构造函数
FileOpenPicker类 具有以下构造函数。
| 构造函数 | 描述 |
|---|---|
| FileOpenPicker | Creates a new instance of a FileOpenPicker. |
方法
The FileOpenPicker 类 具有以下方法。 使用 C#、Visual Basic 和 C++,它还可以继承以下内容中的方法 Object 类.
| 方法 | 描述 |
|---|---|
| PickMultipleFilesAsync | Shows the file picker so that the user can pick multiple files. |
| PickSingleFileAsync | Shows the file picker so that the user can pick one file. |
属性
该 FileOpenPicker类 具有以下属性。
| 属性 | 访问类型 | 描述 |
|---|---|---|
| 读取/写入 | Gets or sets the label text of the file open picker's commit button. | |
| 只读 | Gets the collection of file types that the file open picker displays. | |
| 读取/写入 | Gets or sets the settings identifier associated with the state of the file open picker. | |
| 读取/写入 | Gets or sets the initial location where the file open picker looks for files to present to the user. | |
| 读取/写入 | Gets or sets the view mode that the file open picker uses to display items. |
备注
要开始访问文件和文件夹文件选择器,请参见快速入门:使用文件选择器访问文件。
警告 当您的应用程序已对齐时如果尝试显示文件选择器,文件选择器不会显示,并会引发异常。可以通过确保应用程序未对齐或在调用文件选择器之前展开来避免。 下面的代码示例和文件选择器会为您显示方法。
示例
文件选择器示例演示如何检查应用程序是否已对齐,如何设置文件选择器属性以及如何显示文件选择器,以便用户可以选择某个文件。
if (rootPage.EnsureUnsnapped()) { FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); StorageFile file = await openPicker.PickSingleFileAsync(); if (file != null) { // Application now has read/write access to the picked file OutputTextBlock.Text = "Picked photo: " + file.Name; } else { OutputTextBlock.Text = "Operation cancelled."; } }
对于 C#,文件选择器示例演示如何检查您的应用程序是否在 EnsureUnsnapped 方法对齐。
internal bool EnsureUnsnapped() { // FilePicker APIs will not work if the application is in a snapped state. // If an app wants to show a FilePicker while snapped, it must attempt to unsnap first bool unsnapped = ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap()); if (!unsnapped) { NotifyUser("Cannot unsnap the sample.", NotifyType.StatusMessage); } return unsnapped; }
文件选择器示例还演示了如何显示一个文件选择器,使用户可以选择多个文件。
注意 应始终确保应用程序未对齐(或可将它展开)并且无论用户选择是单个文件或多个文件设置文件选择器属性。
IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync(); if (files.Count > 0) { StringBuilder output = new StringBuilder("Picked files:\n"); // Application now has read/write access to the picked file(s) foreach (StorageFile file in files) { output.Append(file.Name + "\n"); } OutputTextBlock.Text = output.ToString(); } else { OutputTextBlock.Text = "Operation cancelled."; }
要求
|
最低受支持的客户端 | Windows 8 [仅 Windows 应用商店应用] |
|---|---|
|
最低受支持的服务器 | Windows Server 2012 [仅 Windows 应用商店应用] |
|
最低受支持的电话 | Windows Phone 8 |
|
命名空间 |
|
|
元数据 |
|
另请参阅