[英]Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'Windows.Foundation.IAsyncAction'. An explicit conversion exists
So I'm coding this software in C# and I get hit with this error message.所以我正在用 C# 编写这个软件,但我收到了这条错误消息。 For the life of me, I cannot figure it out and searching is not bringing up anything useful, no matter what I do.对于我的生活,我无法弄清楚,无论我做什么,搜索都不会带来任何有用的东西。
The code itself:代码本身:
private async Task LoginIoTHub()
{
TypeJson command_json = new TypeJson();
if (NowNetSta == false) return;
if (IoTHubConnectFlag == false) return;
if (TeamName == null)
{
Debug.WriteLine("Please don't let your team name be void when you call methods 'new IoTHubLocalService(xx,xx,xx)'");
await Task.Delay(TimeSpan.FromSeconds(4));
}
else
{
if (LoginF == false)
{
try
{
Debug.WriteLine("Start to login\r\n");
LoginJson login_msg = new LoginJson();
login_msg.DeviceID = BoardID;
login_msg.name = TeamName;
var messageString = JsonConvert.SerializeObject(login_msg);
command_json.Type = 1;
command_json.Command = messageString;
messageString = JsonConvert.SerializeObject(command_json);
var message = new Message(Encoding.ASCII.GetBytes(messageString));
IAsyncAction Action = deviceClient.SendEventAsync(message);
await Action;
if (Action.Status == AsyncStatus.Completed)
{
Debug.WriteLine("Login success");
LoginF = true;
}
else
{
Debug.WriteLine("Login failed");
await Task.Delay(TimeSpan.FromSeconds(10));
}
}
catch (Exception ex)
{
Debug.WriteLine("Login to IoT Hub failed, please check your team name and internet connection: " + ex.Message);
await Task.Delay(TimeSpan.FromSeconds(10));
}
}
}
}
DeviceClient.SendEventAsync(Message) returns a Task DeviceClient.SendEventAsync(Message)返回一个任务
Sends an event to device hub向设备中心发送事件
public Task SendEventAsync(Message message)
change code to将代码更改为
var Action = deviceClient.SendEventAsync(message);
await Action;
Also note that Task inherits from IAsyncResult
and not IAsyncAction
like in your example.另请注意, Task 继承自IAsyncResult
而不是IAsyncAction
如您的示例。
You can also check completion using Task.IsCompleted Property您还可以使用Task.IsCompleted 属性检查完成情况
if (Action.IsCompleted) { ...
Your problem is here:你的问题在这里:
IAsyncAction Action = deviceClient.SendEventAsync(message);
Async method calls return Tasks.异步方法调用返回任务。 Not IAsyncActions.不是 IAsyncActions。
Assuming the return value of your function call deviceClient.SendEventAsync(message)
is a Task<IAsyncAction>
you should updated your code to this:假设您的函数调用deviceClient.SendEventAsync(message)
的返回值是Task<IAsyncAction>
您应该将代码更新为:
IAsyncAction Action = await deviceClient.SendEventAsync(message);
Reference: https://msdn.microsoft.com/en-us/library/mt674882.aspx参考: https : //msdn.microsoft.com/en-us/library/mt674882.aspx
Your problem is this line:你的问题是这一行:
IAsyncAction Action = deviceClient.SendEventAsync(message);
SendEventAsync return a Task wether you expect a IAsyncAction.无论您期望 IAsyncAction,SendEventAsync 都会返回一个 Task。 If an explicit conversion exists you can do the following:如果存在显式转换,您可以执行以下操作:
IAsyncAction Action = deviceClient.SendEventAsync(message) as IAsyncResult;
But I think SendEventAsync returns Task<IAsyncAction>
inside which means you have to await it:但我认为 SendEventAsync 在里面返回Task<IAsyncAction>
这意味着你必须等待它:
IAsyncAction Action = await deviceClient.SendEventAsync(message);
Based on your comment:根据您的评论:
I tried this, believe it or not.我试过了,信不信由你。 It throws another error though: "Cannot implicitly convert type 'void' to 'Windows.Foundation.IASyncAction'.但它引发了另一个错误:“无法将类型‘void’隐式转换为‘Windows.Foundation.IASyncAction’。
I would guess that you just don't get a IAsyncAction.我猜你只是没有得到 IAsyncAction。 So you have to do it this way:所以你必须这样做:
await deviceClient.SendEventAsync(message);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.