Theo Theo - 1 month ago 4x
C# Question

'Windows.Storage.StorageFile File = await FilePicker.PickSingleFileAsync()' not working

I have the following code that shows a File Picker in an application:

var FilePicker = new Windows.Storage.Pickers.FileOpenPicker();
FilePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
FilePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder;

Windows.Storage.StorageFile File = await FilePicker.PickSingleFileAsync();

Windows.Storage.StorageFile File = await FilePicker.PickSingleFileAsync()
causes this error during compilation:

Error CS4036 'IAsyncOperation<StorageFile>' does not contain a
definition for 'GetAwaiter' and no extension method 'GetAwaiter'
accepting a first argument of type 'IAsyncOperation<StorageFile>'
could be found (are you missing a using directive for 'System'?)

Why is this happening? I got the code from MSDN. Could someone please help me?

Note: I am programming for Universal Windows.


You are missing the obvious reference to System in your usings.

using System;

Why do you need this reference and why it complains it is missing a seemingly unused method?

With await, it actually calls WindowsRuntimeSystemExtensions.GetAwaiter, an extension method over IAsyncOperation (to get the TaskAwaiter to await). Since WindowsRuntimeSystemExtensions resides in the System namespace, you need that using to get the extension method.