我一直在尝试从异步方法返回 Task,它在可移动设备上创建一个文件夹并将其保存以供将来在应用程序中使用。
但是,我得到了可怕的 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
任何帮助将不胜感激,因为我完全困惑这意味着什么,更不用说为什么它不起作用了!
最佳答案
你可以引用这个帖子:https://marcominerva.wordpress.com/2013/03/21/how-to-expose-async-methods-in-a-windows-runtime-component/
基本上将 Task 方法包装为私有(private)方法,并使您的公共(public)方法返回为 IAsyncOperation。
关于c# - 从 Microsoft UWP 上的异步方法返回 Task<string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38030970/