FileOpenPicker 类

定义

表示允许用户选择和打开文件的 UI 元素。

在桌面应用中,在以显示 UI 的方式使用此类实例之前,需要将 对象与其所有者的窗口句柄相关联。 有关详细信息和代码示例,请参阅 显示依赖于 CoreWindow 的 WinRT UI 对象

public ref class FileOpenPicker sealed
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
class FileOpenPicker final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class FileOpenPicker final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class FileOpenPicker final
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public sealed class FileOpenPicker
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class FileOpenPicker
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class FileOpenPicker
function FileOpenPicker()
Public NotInheritable Class FileOpenPicker
继承
Object Platform::Object IInspectable FileOpenPicker
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

文件选取器示例演示如何检查应用是否贴靠、如何设置文件选取器属性以及如何显示文件选取器,以便用户可以选取一个文件。

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.";
}
// Create the picker object and set options
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
// Users expect to have a filtered view of their folders depending on the scenario.
// For example, when choosing a documents folder, restrict the filetypes to documents for your application.
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg"]);

// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
    if (file) {
        // Application now has read/write access to the picked file
        WinJS.log && WinJS.log("Picked photo: " + file.name, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});
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.";
}
openPicker.pickMultipleFilesAsync().then(function (files) {
    if (files.size > 0) {
        // Application now has read/write access to the picked file(s)
        var outputString = "Picked files:\n";
        for (var i = 0; i < files.size; i++) {
            outputString = outputString + files[i].name + "\n";
        }
        WinJS.log && WinJS.log(outputString, "sample", "status");
    } else {
        // The picker was dismissed with no selected file
        WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
    }
});

注解

若要开始访问文件和文件夹文件选取器,请参阅 快速入门:使用文件选取器访问文件

有关如何在 UWP 应用外部使用此 API 的信息,请参阅 从 .NET 应用调用互操作 API

版本历史记录

Windows 版本 SDK 版本 增值
1903 18362 CreateForUser
1903 18362 用户

构造函数

FileOpenPicker()

创建 FileOpenPicker 的新实例。

在桌面应用中,在以显示 UI 的方式使用此类实例之前,需要将 对象与其所有者的窗口句柄相关联。 有关详细信息和代码示例,请参阅 显示依赖于 CoreWindow 的 WinRT UI 对象

属性

CommitButtonText

获取或设置文件打开选取器提交按钮的标签文本。

ContinuationData

获取在 PickSingleFileAndContinuePickMultipleFilesAndContinue 操作之前由应用填充的一组值,这些值停用应用,以便在激活应用时提供上下文。 (Windows Phone 8.x 应用)

FileTypeFilter

获取文件打开选取器显示的文件类型的集合。

SettingsIdentifier

获取或设置与文件打开选取器的状态关联的设置标识符。

SuggestedStartLocation

获取或设置文件打开选取器查找要呈现给用户的文件的初始位置。

User

获取有关为其创建了 FileOpenPicker 的用户的信息。 将此属性用于 多用户应用程序

ViewMode

获取或设置文件打开选取器用于显示项的视图模式。

方法

CreateForUser(User)

创建一个 FileOpenPicker ,其范围限定为指定用户的个人目录。 将此方法用于 多用户应用程序

PickMultipleFilesAndContinue()

自 Windows 10 起已过时;请改用 PickSingleFolderAsync。 显示文件选取器,以便用户可以选取多个文件,停用和应用,并在操作完成时重新激活它。 (Windows Phone 8.x 应用)

PickMultipleFilesAsync()

显示文件选取器,以便用户可以选取多个文件。 (UWP 应用)

PickSingleFileAndContinue()

显示文件选取器,以便用户可以选取一个文件,这可能会停用应用并在操作完成时重新激活它。 (Windows Phone 8.x 应用)

PickSingleFileAsync()

显示文件选取器,以便用户可以选取一个文件。

PickSingleFileAsync(String)

显示文件选取器,以便用户可以选取一个文件。

ResumePickSingleFileAsync()

如果用户在完成选取器操作并暂停应用后离开应用,则恢复选取操作以检索用户选择的文件。

适用于

另请参阅