操作员!不能应用于Task< bool>类型的操作数 [英] Operator ! cannot be applied to operand of type Task<bool>

查看:122
本文介绍了操作员!不能应用于Task< bool>类型的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实现,并在 ChangeDate()方法中添加了 Async Task< bool> 操作只是布尔

I have following implementation and added a Async Task<bool> operation in the ChangeDate() method, previously it was just bool.

在以下行中 if(!ChangeDate())


操作员!不能应用于
类型的操作数Task

Operator ! cannot be applied to operand of type Task



 public DateTime Date
 {
    get { return _date; }
    set
    {
      if (!ChangeDate())
      {
         return;
      }

      _date = value.Date;

    }
} 

private async Task<bool> ChangeDate()
{
  if (IsSave)
  {
     await Mvx.Resolve<IUserDialogs>().ConfirmAsync(new ConfirmConfig
     {
        Message = "Are you sure ?",
        OnConfirm = b =>
        {
            if (b)
            {
              Save();
            }
         }
      });
   }
   return true;
 }


推荐答案

也许您想要这样的东西...?

Maybe you want something like this ... ?

public DateTime Date { get; private set; }

public Task SetDateIfUserConfirmsAsync( DateTime proposedDate) 
{ 
     var confirmConfig = new ConfirmConfig() {
         Message = "Are you sure ?",
         OnConfirm = b => { if (b) { this.Date = proposedDate; } }
     }
     await Mvx.Resolve<IUserDialogs>().ConfirmAsync( confirmConfig);
}

您尚未说出正在使用什么UI,但可能您在按钮回调,它具有同步签名(无效返回)。在那种情况下,您要确保捕获任何异常,因为否则您将永远看不到它们(异常不会传播回异步void方法的调用方)。像这样:

You haven't said what UI you are using but likely you are in a button callback, which has a synchronous signature (void return). In that case you want to make sure that you catch any exceptions because otherwise you will never see them (exceptions are not propagated back to the caller of an async void method). A bit like this:

public async void OnButtonClick()
{ 
    try {
        DateTime proposedDate = ...;
        await SetDateIfUserConfirmsAsync( proposedDate)
    } catch (Exception e) {
        // display or log the exception
    }
 }

PS对于您看到的错误,操作符!不能应用于Task< bool>类型的操作数,这是因为您没有等待任务。如果t具有Task< bool>类型,那么 await t的类型为bool。

P.S. On the error you saw, "operator ! cannot be applied to operand of type Task<bool>", it was because you did not await the task. If t has type Task<bool> then "await t" has type bool.

这篇关于操作员!不能应用于Task&lt; bool&gt;类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆