系统线程任务字符串到字符串转换错误



我试图将firebase上的照片下载到listView中,具体取决于候选。

我有一个任务列表链接为listView的数据源,并且可以从firebase检索URL的函数。

我尝试在任务列表中添加一个字符串以检索URL,但是我收到了一个错误消息,说我不能将System.Threading.Tasks String转换为字符串。

    public async Task<string> GetFile(string fileName)
    {      
        return await GlobalVar.firebaseStorage
            .Child("ProfilePhotos")
            .Child(fileName)
            .GetDownloadUrlAsync();
    }
    public async Task<List<JobApplication>> GetApplications()
    {
        var canInfo = await GetCandidates();
        return (await GlobalVar.firebaseClient
          .Child("JobApplication")
          .OnceAsync<JobApplication>()).Select(item => new JobApplication
          {
              CandidateID = item.Object.CandidateID,
              ApplicationDate = item.Object.ApplicationDate,
              ApplicationTime = item.Object.ApplicationTime,
              CandidateName = canInfo.Find(a => a.CandidateId == item.Object.CandidateID).Name + " " + canInfo.Find(a => a.CandidateId == item.Object.CandidateID).LastName,
              NewCanID = item.Object.CandidateID.Substring(1),
              ImageURL = GetFile(item.Object.CandidateID.Substring(1) + "-One.jpg")
          }).ToList();
    }

我已经在图像源中使用了est表达式,但是'getfile(item.object.candidateid.substring(1( " -One.jpg"(下面有一个红线。无论如何,我可以将其转换为有效的字符串,以便它可以工作吗?

我收到一条错误消息,说我不能转换system.threading.tasks字符串到字符串。

Visual Studio会给您这个错误,因为您正在尝试将Task<string>分配给string,并且找不到转换。Task<T>包装T,您需要解开T。从Task<T>解开T的正确方法是使用await

在这种情况下,我认为分解庞大的return语句会更容易,然后您可以看到需要更清楚地做的事情:

public async Task<List<JobApplication>> GetApplications()
{
  var canInfo = await GetCandidates();
  var jobApplication = await GlobalVar.firebaseClient
          .Child("JobApplication")
          .OnceAsync<JobApplication>();
  return jobApplication.Select(item => new JobApplication
  {
    CandidateID = item.Object.CandidateID,
    ApplicationDate = item.Object.ApplicationDate,
    ApplicationTime = item.Object.ApplicationTime,
    CandidateName = canInfo.Find(a => a.CandidateId == item.Object.CandidateID).Name + " " + canInfo.Find(a => a.CandidateId == item.Object.CandidateID).LastName,
    NewCanID = item.Object.CandidateID.Substring(1),
    ImageURL = GetFile(item.Object.CandidateID.Substring(1) + "-One.jpg") // error
  }).ToList();
}

您需要在从GetFile返回的Task<string>上使用await,这需要将lambda传递给Selectasync

public async Task<List<JobApplication>> GetApplications()
{
  var canInfo = await GetCandidates();
  var jobApplication = await GlobalVar.firebaseClient
          .Child("JobApplication")
          .OnceAsync<JobApplication>();
  return jobApplication.Select(async item => new JobApplication
  {
    CandidateID = item.Object.CandidateID,
    ApplicationDate = item.Object.ApplicationDate,
    ApplicationTime = item.Object.ApplicationTime,
    CandidateName = canInfo.Find(a => a.CandidateId == item.Object.CandidateID).Name + " " + canInfo.Find(a => a.CandidateId == item.Object.CandidateID).LastName,
    NewCanID = item.Object.CandidateID.Substring(1),
    ImageURL = await GetFile(item.Object.CandidateID.Substring(1) + "-One.jpg")
  }).ToList();
  // error: the code is now trying to return Task<List<Task<JobApplication>>>
}

现在,您有一个从Select返回的任务序列。您也可以(异步(等待它们一次完成:

public async Task<List<JobApplication>> GetApplications()
{
  var canInfo = await GetCandidates();
  var jobApplication = await GlobalVar.firebaseClient
          .Child("JobApplication")
          .OnceAsync<JobApplication>();
  var result = new List<JobApplication>();
  foreach (var item in jobApplication)
  {
    result.Add(new JobApplication
    {
      CandidateID = item.Object.CandidateID,
      ApplicationDate = item.Object.ApplicationDate,
      ApplicationTime = item.Object.ApplicationTime,
      CandidateName = canInfo.Find(a => a.CandidateId == item.Object.CandidateID).Name + " " + canInfo.Find(a => a.CandidateId == item.Object.CandidateID).LastName,
      NewCanID = item.Object.CandidateID.Substring(1),
      ImageURL = await GetFile(item.Object.CandidateID.Substring(1) + "-One.jpg")
    });
  }
  return result;
}

或者,您可以同时启动它们,然后等待它们全部完成:

public async Task<List<JobApplication>> GetApplications()
{
  var canInfo = await GetCandidates();
  var jobApplication = await GlobalVar.firebaseClient
          .Child("JobApplication")
          .OnceAsync<JobApplication>();
  var tasks = jobApplication.Select(async item => new JobApplication
  {
    CandidateID = item.Object.CandidateID,
    ApplicationDate = item.Object.ApplicationDate,
    ApplicationTime = item.Object.ApplicationTime,
    CandidateName = canInfo.Find(a => a.CandidateId == item.Object.CandidateID).Name + " " + canInfo.Find(a => a.CandidateId == item.Object.CandidateID).LastName,
    NewCanID = item.Object.CandidateID.Substring(1),
    ImageURL = await GetFile(item.Object.CandidateID.Substring(1) + "-One.jpg")
  }).ToList();
  var result = await Task.WhenAll(tasks);
  return result.ToList();
}

相关内容

  • 没有找到相关文章

最新更新