gpt4 book ai didi

c# - 运算符(operator) !不能应用于 Task 类型的操作数

转载 作者:行者123 更新时间:2023-12-05 08:20:31 30 4
gpt4 key购买 nike

我有以下实现并添加了一个 Async Task<bool>ChangeDate() 中运行方法,以前它只是 bool .

在下一行if (!ChangeDate())

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;
}

最佳答案

找到这个答案:How to make an Asynchronous Method return a value?

您需要创建一个异步辅助方法,然后调用它而不是只调用您的 setter,因为正如@Ron Beyer 指出的那样,await 调用在属性中不起作用。

替换:

if(!ChangeDate())

与:

if( !(await ChangeDate()) )

关于c# - 运算符(operator) !不能应用于 Task<bool> 类型的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48757425/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com