WinRT项目要求使用安全类型,所以Task类型函数无法直接暴露
编译出错:
Method ‘MyRuntimeComponent.MyLibrary.GetUriContent(System.String)’ has a parameter of type ‘System.Threading.Tasks.Task’ in its signature. Although this generic type is not a valid Windows Runtime type, the type or its generic parameters implement interfaces that are valid Windows Runtime types. Consider changing the type ‘Task’ in the method signature to one of the following types instead: Windows.Foundation.IAsyncAction, Windows.Foundation.IAsyncOperation, or one of the other Windows Runtime async interfaces. The standard .NET awaiter pattern also applies when consuming Windows Runtime async interfaces. Please see System.Runtime.InteropServices.WindowsRuntime.AsyncInfo for more information about converting managed task objects to Windows Runtime async interfaces.
一般的只需要将Task的权限从public 改为 private即可,但如果非要暴露Task,做法如下:
public Windows.Foundation.IAsyncOperation<List<string>> GetUriContentAsync(int index, int picNumber) { return GetBingImg(index,picNumber).AsAsyncOperation(); } private static async Task<List<string>> GetBingImg(int index, int picNumber) { ... }
纠正,List<string>