最近研究MS的UWP,发现虽然平台统一了,但是.net的API却变化很大,很多以前的常用的系统库函数、类都被砍掉了,比如读写文件的System.IO.FileStream,UWP下要改用StorageFile,结合异步操作async, await倒还算方便,但看完文档后一试:
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(SAVE_FILE, CreationCollisionOption.ReplaceExisting);
就报了错:
error CS4036: ‘IAsyncOperation
‘ does not contain a definition for ‘GetAwaiter’ and no extension method ‘GetAwaiter’ accepting a first argument of type ‘IAsyncOperation ‘ could be found (are you missing a using directive for ‘System’?)
虽然看提示也发现了原来是缺少”using System;”,究其原因,原来是这样:
await关键字的出现会触发编译器调用WindowsRuntimeSystemExtensions.GetAwaiter,是CreateFileAsync返回的泛型类IAsyncOperation的扩展方法(从而获得TaskAwaiter来进行await),而WindowsRuntimeSystemExtensions是存在于System命名空间下的,所以对System的using是必不可少的。