【问题标题】:How to have return type from async如何从异步获得返回类型
【发布时间】:2016-10-17 22:38:26
【问题描述】:

我正在使用下面的代码从异步中获取返回类型,但它会引发错误

async Task<double> getDuration(string fileFullPath)
{    
    Windows.Storage.StorageFolder localRoot = Windows.Storage.ApplicationData.Current.LocalFolder;
    Windows.Storage.StorageFolder mp3 = await localRoot.GetFolderAsync(Path.GetDirectoryName(fileFullPath));
    Windows.Storage.StorageFile file = await mp3.GetFileAsync(Path.GetFileName(fileFullPath));
    Windows.Storage.FileProperties.MusicProperties music = await file.Properties.GetMusicPropertiesAsync();
    return music.Duration.TotalSeconds;
}

错误:

找不到类型或命名空间名称“任务”(您是否缺少 using 指令还是程序集引用?)

如何获得异步的返回值?

【问题讨论】:

  • 你有命名空间using System.Threading.Tasks; 吗?如果你不添加这个命名空间..让我知道它是否有效..
  • 非常感谢,我从一小时开始就一直在寻找这个命名空间 :)
  • 查看链接以了解有关在 Visual Studio 中添加命名空间的更多信息 [stackoverflow.com/questions/148977/…]

标签: c# windows-phone-8


【解决方案1】:

找不到类型或命名空间名称“任务”(您是否缺少 using 指令还是程序集引用?)

我认为你没有添加命名空间

System.Threading.Tasks

如何获得异步的返回值?

示例代码

public async Task<int> ExampleMethodAsync()
{
    var httpClient = new HttpClient();
    int exampleInt = (await httpClient.GetStringAsync("http://msdn.microsoft.com")).Length;
    ResultsTextBox.Text += "Preparing to finish ExampleMethodAsync.\n";
    // After the following return statement, any method that's awaiting
    // ExampleMethodAsync (in this case, StartButton_Click) can get the 
    // integer result.
    return exampleInt;
}

【讨论】:

    猜你喜欢
    • 2019-06-04
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 2020-11-27
    相关资源
    最近更新 更多