好的,我的雇员对象有一个EmployeePayHistories列表,这些列表显示在数据网格中。我有一种方法可以从数据网格中获取选定的行,在这里我试图实际实现该功能。但是在DeleteEmployeePayHistories()部分,它给了我一个错误,说它是可等待的。我很想知道如何修复它,以及为什么要这样做。在此先感谢您的帮助。

    private Task DeleteEmployeePayHistories()
        {
           Employee.EmployeePayHistories.Remove(SelectedPayHistory);
           SelectedPayHistory = null;
        }

最佳答案

您的方法应该返回Task(可能是Task.CompletedTask),或者使用async关键字和await

private Task DeleteEmployeePayHistories()
{
    Employee.EmployeePayHistories.Remove(SelectedPayHistory);
    SelectedPayHistory = null;
    return Task.CompletedTask;
}


要么

private async Task DeleteEmployeePayHistories()
{
    // something in here should use await or you'll get another warning
    Employee.EmployeePayHistories.Remove(SelectedPayHistory);
    SelectedPayHistory = null;
}


最后,如果您不需要异步代码,只需将您的方法设置为void

private void DeleteEmployeePayHistories()
{
    Employee.EmployeePayHistories.Remove(SelectedPayHistory);
    SelectedPayHistory = null;
}

09-11 15:09