UWP、C++/Cx FileOpenPicker

UWP, C++/Cx FileOpenPicker

有谁知道我做错了什么。

    // STORAGE FILE
    StorageFile^ saveFile;

    // FILE PICKER, FOR SELECTING A SAVE FILE
    FileOpenPicker^ filePicker = ref new FileOpenPicker;

    // ARRAY OF FILE TYPES
    Array<String^>^ fileTypes = ref new Array<String^>(1);
    fileTypes->Data[0] = ".txt";

    filePicker->ViewMode = PickerViewMode::Thumbnail;
    filePicker->SuggestedStartLocation = PickerLocationId::Desktop;
    filePicker->FileTypeFilter->ReplaceAll(fileTypes);

    // THIS SHOULD HOPEFULLY LET US PICK A FILE

    saveFile = filePicker->PickSingleFileAsync();

特别是最后一行:

saveFile = filePicker->PickSingleFileAsync();

我收到以下错误。

error C2440: '=': cannot convert from 'Windows::Foundation::IAsyncOperation ^' to 'Windows::Storage::StorageFile ^'

error C2440: '=': cannot convert from 'Windows::Foundation::IAsyncOperation ^' to 'Windows::Storage::StorageFile ^'

PickSingleFileAsync是异步方法,return类型是Windows::Foundation::IAsyncOperation,不能转成StorageFile类型。正如 Hans Passant 所说,您可以使用 create_task() 来等待此异步操作。

create_task(folderPicker->PickSingleFolderAsync()).then([this](StorageFolder^ folder)
{
    if (folder)
    {
        //do some stuff
    }
    else
    {
        //do some stuff
    }
});

更多请参考Asynchronous programming in C++