0

Does anyone know what I'm doing wrong with this.

    // 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();

specifically the last line:

saveFile = filePicker->PickSingleFileAsync();

I get the following error.

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

Gavin Hannah
  • 161
  • 9
  • 2
    You have to await any async operation. Use create_task() or the co_await extension keyword. Lots of example code out there. – Hans Passant Jul 24 '17 at 16:50
  • 1
    You can also just append the file type to the existing vector -- you don't have to replace the contents with your own array. – Peter Torr - MSFT Jul 25 '17 at 06:34

1 Answers1

1

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

PickSingleFileAsync is asynchronous method, and the return type is Windows::Foundation::IAsyncOperation,it can't be convert to StorageFile type. As Hans Passant said you could use create_task() to await this async operation.

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

For more please refer to Asynchronous programming in C++.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36