个人资料

跳过导航链接首页 > 博客列表 > 博客正文

如何在WinRT(windows runtime)项目使用Task

分类:

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>也应该替换为IEnumerable<string>

admin
最初发表2019/9/28 1:01:55 最近更新2019/9/28 1:15:52 1276
为此篇作品打分
10
   评论