我收到来自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());
答案 0 :(得分:1)
使用await时 - 必须在支持异步操作的方法中使用它。当你返回一个值时 - 将其设为Task<>
尝试使您的方法异步:
private async Task<AudioVideoCaptureDevice> SetupController()
{
// your code
}
因为它现在是异步的,所以使用它:
AudioVideoCaptureDevice myDevice = await SetupController(); // which should also be run from async method