简体   繁体   English

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

[英]Operator ! cannot be applied to operand of type Task<bool>

I have following implementation and added a Async Task<bool> operation in the ChangeDate() method, previously it was just bool . 我有以下实现,并在ChangeDate()方法中添加了Async Task<bool>操作,以前它只是bool

In the following line if (!ChangeDate()) 在下面的行中, if (!ChangeDate())

Operator ! 操作员! cannot be applied to operand of type Task 不能应用于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);
}

You haven't said what UI you are using but likely you are in a button callback, which has a synchronous signature (void return). 您尚未说明正在使用什么UI,但可能处于按钮回调中,该回调具有同步签名(无效返回)。 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). 在那种情况下,您要确保捕获任何异常,因为否则您将永远看不到它们(异常不会传播回异步void方法的调用方)。 A bit like this: 有点像这样:

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

PS On the error you saw, "operator ! cannot be applied to operand of type Task<bool>", it was because you did not await the task. PS在看到的错误上,“运算符!无法应用于Task <bool>类型的操作数”,这是因为您没有等待任务。 If t has type Task<bool> then "await t" has type bool. 如果t的类型为Task <bool>,则“ await t”的类型为bool。

Found this answer: How to make an Asynchronous Method return a value? 找到了这个答案: 如何使一个异步方法返回一个值?

You'll need to make an async helper method, and call that instead of calling just your setter, because as @Ron Beyer points out, the await call will not work in a property. 您需要制作一个异步帮助器方法,然后调用该方法,而不是仅调用您的setter方法,因为正如@Ron Beyer指出的那样,await调用不适用于属性。

Replace: 更换:

if(!ChangeDate())

With: 带有:

if( !(await ChangeDate()) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算符&#39;&amp;&amp;&#39;不能应用于&#39;bool&#39;和&#39;int&#39;类型的操作数 - Operator '&&' cannot be applied to operand of type 'bool' and 'int' 运算符“ &amp;&amp;”不能应用于“任务”类型的操作数 <bool> &#39;和&#39;布尔&#39; - Operator '&&' cannot be applied to operands of type 'Task<bool>' and 'bool' 运营商&#39;||&#39; 不能应用于&#39;bool&#39;类型的操作数? 和&#39;布尔?&#39; - Operator '||' cannot be applied to operands of type 'bool?' and 'bool?' 运算符“&gt;”不能应用于类型为“ bool”和“ bool”的操作数 - Operator '>' cannot be applied to operands of type 'bool' and 'bool' 运算符 || 不能应用于“bool”和“bool”类型的操作数? - Operator || cannot be applied to operands of type "bool" and "bool?" 接线员'?'不能应用于'方法组'类型的操作数 - Operator '?' cannot be applied to operand of type 'method group' 接线员&#39;!&#39; 不能应用于x类型的操作数 - Operator '!' cannot be applied to operand of type x 运算符“ ”不能应用于类型的操作数<int>和空 - Operator ' ' cannot be applied to operand of type <int> and null 运算符&#39;==&#39;不能应用于&#39;方法组&#39;类型的操作数 - Operator '==' cannot be applied to operand of type 'method group' 错误:操作员&#39;!&#39; 不能应用于&#39;int&#39;类型的操作数 - Error: Operator '!' cannot be applied to operand of type 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM