简体   繁体   English

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

[英]Syntax error when trying to await “(awaitable) Windows.Foundation.IAsyncOperation”

I'm getting a really confusing error Message from Visual Studio, telling me I cannot use "await" on a method that is marked "(awaitable)." 我收到来自Visual Studio的真正令人困惑的错误消息,告诉我不能在标记为“(awaitable)”的方法上使用“ await”。 Here is the full message: 这是完整的消息:

Error 1 The 'await' operator can only be used within an async method. 错误1“等待”运算符只能在异步方法中使用。 Consider marking this method with the 'async' modifier and changing its return type to 'Task<Windows.Phone.Media.Capture.AudioVideoCaptureDevice>' 考虑使用'async'修饰符标记此方法并将其返回类型更改为'Task<Windows.Phone.Media.Capture.AudioVideoCaptureDevice>'

And here is my code: 这是我的代码:

    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;
    }

The error is with this line: 错误是与此行:

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

When you are using await - you must use it within a method that supports asynchronous operation. 使用等待时-必须在支持异步操作的方法中使用它。 And as you are returning a value - make it a Task<> 在返回值时-将其设为Task<>

Try to make your method async: 尝试使您的方法异步:

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

And as it's now asynchronous then use it: 由于它现在是异步的,因此可以使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM