Tuesday, June 4, 2013

Windows Phone 8, WP8 - The 'await' operator can only be used within an async method.

Take this code for example: private void OnUnlockCommand(object parameter) { StorageFile file = await Windows.Storage.ApplicationData. Current.TemporaryFolder.CreateFileAsync("filename", CreationCollisionOption.ReplaceExisting); } The code above will fail with the error message: Error 2 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. To solve this you must add async to the method declaration like below: private async void OnUnlockCommand(object parameter) { StorageFile file = await Windows.Storage.ApplicationData. Current.TemporaryFolder.CreateFileAsync("filename", CreationCollisionOption.ReplaceExisting); }