FileOpenPicker Class

代表 UI 項目,該項目可讓使用者選擇與開啟檔案。

語法


var fileOpenPicker = new Windows.Storage.Pickers.FileOpenPicker();

屬性

[Muse()]
[Version(0x06020000)]

成員

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類別 擁有這些屬性。

屬性存取類型描述

CommitButtonText

讀取/寫入Gets or sets the label text of the file open picker's commit button.

FileTypeFilter

唯讀Gets the collection of file types that the file open picker displays.

SettingsIdentifier

讀取/寫入Gets or sets the settings identifier associated with the state of the file open picker.

SuggestedStartLocation

讀取/寫入Gets or sets the initial location where the file open picker looks for files to present to the user.

ViewMode

讀取/寫入Gets or sets the view mode that the file open picker uses to display items.

 

備註

若要開始存取檔案和資料夾檔案選擇器,請參閱快速入門:使用檔案選擇器存取檔案

重要事項  在 Windows 8 中,如果您嘗試在應用程式貼齊時顯示檔案選擇器,檔案選擇器將不會顯示,而且會擲回例外狀況。您可以藉由確保應用程式不會貼齊或是在呼叫檔案選擇器之前取消貼齊的方式,避免這種情況發生。 下列程式碼範例和檔案選擇器範例會示範使用方式。請注意,Windows 8.1 不會定義特定貼齊的視窗大小。相反地,使用者可以調整應用程式大小到所有寬度,向下到最小值。因此,如果您的應用程式只會部署在 Windows 8.1 上,您可以忽略這個主題的範例程式碼中的 EnsureUnsnapped 函式和它的呼叫。

Windows Phone 8

只有原生應用程式支援這個 API 。

範例

檔案選擇器範例示範如何檢查應用程式是否貼齊,如何設定檔案選擇器屬性,以及如何顯示檔案選擇器,讓使用者可以選取一個檔案。


// Verify that we are currently not snapped, or that we can unsnap to open the picker
var currentState = Windows.UI.ViewManagement.ApplicationView.value;
if (currentState === Windows.UI.ViewManagement.ApplicationViewState.snapped &&
    !Windows.UI.ViewManagement.ApplicationView.tryUnsnap()) {
    // Fail silently if we can't unsnap
    return;
}

// 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");
    }
});


使用 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;
}


檔案選擇器範例也會示範如何顯示檔案選擇器,讓使用者可以選取多個檔案。

注意事項  無論使用者選擇單一檔案或多個檔案,您都應該確認應用程式並未貼齊 (或是可取消貼齊),並且設定檔案選擇器屬性。


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");
    }
});


您也可以在 Windows Phone 使用檔案選擇器。以下顯示 C++ 範例。在 Windows Phone,只有在原生應用程式才支援 FileOpenPicker


FileOpenPicker^ openPicker = ref new FileOpenPicker(); 
openPicker->ViewMode = PickerViewMode::Thumbnail; 
openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary; 
openPicker->FileTypeFilter->Append("*"); 
   
create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) 
{
    if (file) 
    { 
        m_PickedFileName = file->Name; 
    } 
    else 
    { 
        // Operation cancelled.
    } 
});


需求

支援的最小用戶端

Windows 8 [僅限 Windows 市集應用程式]

支援的最小伺服器

Windows Server 2012 [僅限 Windows 市集應用程式]

支援的最小電話

Windows Phone 8

命名空間

Windows.Storage.Pickers
Windows::Storage::Pickers [C++]

Metadata

Windows.winmd

請參閱

檔案選擇器範例
快速入門:使用檔案選擇器存取檔案

 

 

顯示:
© 2014 Microsoft