从 Microsoft UWP 上的异步方法返回 Task<string>

Posted

技术标签:

【中文标题】从 Microsoft UWP 上的异步方法返回 Task<string>【英文标题】:returning Task<string> from async method on Microsoft UWP 【发布时间】:2016-10-28 02:39:15 【问题描述】:

我一直在尝试从异步方法返回任务,它在可移动设备上创建一个文件夹并将其保存以供将来在应用程序中使用。 但是,我得到了可怕的 WME1039,说我没有使用有效的 Windows 运行时类型。我在这里检查了有效的运行时类型: Windows Runtime base data types,并且字符串是有效类型.. 我完全被卡住了,真的不知道从这里去哪里!我是否缺少异步/等待模式的基本内容?下面列出了我当前的代码,请原谅它的粗糙,我现在只是在补充这个概念!

调用代码:

await LoadExtDrive();

方法:

public async Task<string> LoadExtDrive()

    StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
    // Get the first child folder, which represents the SD card.
    IReadOnlyList<StorageFolder> tmp;
    try
    
        tmp = await externalDevices.GetFoldersAsync();
    
    catch (Exception ex)
    
        throw;
    
    StorageFolder sdCard = ( tmp).FirstOrDefault();
    if (sdCard != null)
    
     // An Removable device is present..
     var dbdir = 
     await sdCard.CreateFolderAsync(APP_DB_DIR_NAME, CreationCollisionOption.OpenIfExists);
     var dirToken = 
     Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(dbdir);
     return dirToken;
    
    else
    
        // No SD card is present.
        return null;
    

以及构建错误:

Error   WME1039 Method 'WebSocketService.StartupTask.LoadExtDrive()' has a parameter of 
type 'System.Threading.Tasks.Task<System.String>' 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.WebSocketService

任何帮助将不胜感激,因为我完全困惑这意味着什么,更不用说为什么它不起作用了!

【问题讨论】:

string 是有效的 WinRT 类型,但 Task&lt;&gt; 不是。您可以在 System.Runtime.WindowsRuntime 程序集中找到一个 AsAsyncAction 方法,该方法可以将 .NET Task 包装为 IAsyncAction。更多信息here. 谢谢,我已经把它包起来了:​​public IAsyncOperation&lt;string&gt; LoadDriveAsync() Task&lt;string&gt; load = LoadExtDrive(); IAsyncOperation&lt;string&gt; to = load.AsAsyncOperation(); return to; 并且还把上面的“LoadExtDrive()”变成了一个私有方法,这似乎已经解决了这个问题,虽然有一个包装器似乎有点笨重对于另一种方法。我会继续调查,谢谢你的帮助! 这有点笨拙,但在公开 API/ABI 以以语言惯用的方式实现事物(即使用 Task&lt;&gt;)然后公开更适合 API(即`IAsyncOperation`)。如果您发现您的包装代码变得笨拙,您可能会考虑将其与实现完全分离,并制作您自己的包装层。 @SeanCline 你能把关于 IAsyncAction (和链接)的第一条评论作为答案,这样我就可以给你功劳了吗?你让我摆脱困境,看起来很公平! 【参考方案1】:

你可以参考这个帖子:https://marcominerva.wordpress.com/2013/03/21/how-to-expose-async-methods-in-a-windows-runtime-component/

基本上将 Task 方法包装为私有方法,并使您的公共方法返回为 IAsyncOperation。

【讨论】:

以上是关于从 Microsoft UWP 上的异步方法返回 Task<string>的主要内容,如果未能解决你的问题,请参考以下文章

如何从 SoftwareBitmap (UWP) 正确释放 BitmapBuffer?

win 10 UAP/UWP(通用应用程序)的异步套接字库?

UWP - 背景图像上的淡入效果

C# 异步与Windows应用程序

UWP开发:网络请求数据返回:Id =42 , Status = WaitingForActivation, Method = “{null}”, Result = “{Not yet compute

如何更新 UWP Microsoft Toolkit DataGrid 中的单元格值?