尝试等待“(等待)Windows.Foundation.IAsyncOperation”时出现语法错误

时间:2014-02-15 22:09:13

标签: c# .net windows asynchronous windows-phone-8

我收到来自Visual Studio的一个非常令人困惑的错误消息,告诉我我不能在标记为“(等待)”的方法上使用“await”。以下是完整的信息:

  

错误1“await”运算符只能在异步方法中使用。请考虑使用“async”修饰符标记此方法,并将其返回类型更改为'Task<Windows.Phone.Media.Capture.AudioVideoCaptureDevice>'

这是我的代码:

    private AudioVideoCaptureDevice SetupController()
    {
        CameraSensorLocation Location = CameraSensorLocation.Back;
        AudioVideoCaptureDevice CameraController = await AudioVideoCaptureDevice.OpenAsync(Location, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(Location).First());

        CameraController.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

        return CameraController;
    }

错误在于这一行:

AudioVideoCaptureDevice CameraController = await AudioVideoCaptureDevice.OpenAsync(Location, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(Location).First());

1 个答案:

答案 0 :(得分:1)

使用await时 - 必须在支持异步操作的方法中使用它。当你返回一个值时 - 将其设为Task<>

尝试使您的方法异步:

private async Task<AudioVideoCaptureDevice> SetupController()
{
  // your code
}

因为它现在是异步的,所以使用它:

AudioVideoCaptureDevice myDevice = await SetupController(); // which should also be run from async method