Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回任务<;字符串>;从Microsoft UWP上的异步方法_C#_.net_Asynchronous_Uwp_Windows 10 Iot Core - Fatal编程技术网

C# 返回任务<;字符串>;从Microsoft UWP上的异步方法

C# 返回任务<;字符串>;从Microsoft UWP上的异步方法,c#,.net,asynchronous,uwp,windows-10-iot-core,C#,.net,Asynchronous,Uwp,Windows 10 Iot Core,我一直在尝试从异步方法返回任务,它会在可移动设备上创建一个文件夹,并将其保存以备将来在应用程序中使用。 然而,我得到了可怕的WME1039,说我没有使用有效的Windows运行时类型。我已在此处检查有效的运行时类型: ,并且字符串是有效类型。。 我完全被卡住了,真的不知道从这里到哪里去!我是否缺少异步/等待模式的基本功能?下面列出了我当前的代码,请原谅它的粗糙,我现在只是在填充这个概念 呼叫代码: await LoadExtDrive(); 方法: public async Task<s

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

呼叫代码:

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;
    }
}
public异步任务LoadExtDrive()
{
StorageFolder externalDevices=Windows.Storage.KnownFolders.RemovableDevices;
//获取第一个子文件夹,它表示SD卡。
IReadOnlyList tmp;
尝试
{
tmp=await externalDevices.GetFoldersAsync();
}
捕获(例外情况除外)
{
投掷;
}
StorageFolder sdCard=(tmp).FirstOrDefault();
如果(SD卡!=null)
{
//存在可移动设备。。
var dbdir=
wait sdCard.CreateFolderAsync(APP_DB_DIR_NAME,CreationCollisionOption.OpenIfExists);
var dirToken=
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(dbdir);
返回dirToken;
}
其他的
{
//没有SD卡。
返回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
错误WME1039方法“WebSocketService.StartupTask.LoadExtDrive()”的参数为
在签名中键入“System.Threading.Tasks.Task”。虽然这是通用的
类型不是有效的Windows运行时类型,该类型或其泛型参数将实现
属于有效Windows运行时类型的接口。考虑更改“任务”类型
在方法中,将签名改为以下类型之一:
Field.IasyccActudio,Windows .Fastual.IasycCcor,或其中之一
其他Windows运行时异步接口。标准的.NET等待者模式也是如此
在使用Windows运行时异步接口时应用。请看
有关详细信息,请参阅System.Runtime.InteropServices.WindowsRuntime.AsyncInfo
关于将托管任务对象转换为Windows运行时异步
interfaces.WebSocketService

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

你可以参考这篇文章:


基本上将任务方法包装为私有方法,并使您的公共方法以IAsyncOperation的形式返回。

string
是有效的WinRT类型,但
Task
不是。您可以在
System.Runtime.WindowsRuntime
程序集中找到一个方法,该方法可以将.NET
任务
包装为一个。更多信息。为此,我将其包装成:
public IAsyncOperation LoadDriveAsync(){Task load=LoadExtDrive();IAsyncOperation to=load.asasAsAsAsAsyncOperation();return to;}
并将上面的“LoadExtDrive()”作为私有方法,似乎解决了这个问题,不过,为另一个方法提供包装似乎有点笨重。我会继续调查的,谢谢你的帮助!这有点笨拙,但公开API/ABI以该语言惯用的方式(即使用
Task
)实现事物,然后公开更适合API的内容(即“IAsyncOperation”)时肯定并不少见。如果您发现您的包装代码变得笨拙,您可以考虑将其从实现中完全分离出来,并创建自己的包装层。@SeanCline能否将关于iSyncAction(和链接)的第一条注释作为答案,以便我可以给您评分?你把我从困境中救了出来,而且看起来很公平!