Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
FilePicker.FileTypeFilter.Add(".pcs");
FilePicker.FileTypeFilter.Add(".pcp");

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

However, 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.

like image 724
Theo Avatar asked Dec 31 '15 12:12

Theo


1 Answers

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.

like image 172
Patrick Hofman Avatar answered Nov 14 '22 21:11

Patrick Hofman