【问题标题】:InitializeAsync in MediaCapture doesnt workMediaCapture 中的 InitializeAsync 不起作用
【发布时间】:2014-12-12 16:31:44
【问题描述】:

当我这样做时:

try
            {
                MediaCapture mc = new MediaCapture();
                await mc.InitializeAsync();

                if (mc.VideoDeviceController.TorchControl.Supported == true)
                {
                    mc.VideoDeviceController.TorchControl.Enabled = true;
                    if (mc.VideoDeviceController.TorchControl.PowerSupported == true)
                    {
                        mc.VideoDeviceController.TorchControl.PowerPercent = 100;
                    }
                }
            }
            catch(Exception ex)
            {
                //TODO: Report exception to user
            }

我收到了这个错误:

错误 1 ​​'await' 运算符只能在异步方法中使用。考虑使用“async”修饰符标记此方法并将其返回类型更改为“Task”。

所以我尝试'await async mc.InitializeAsync();',然后我得到了这个错误:

'错误1;预计'

我做错了什么?也许图书馆有问题?谢谢

【问题讨论】:

  • 您需要学习 C# 中 async 关键字的基础知识。
  • 是我想要做的。我还没有使用它。
  • 您需要将 async 关键字添加到您的方法声明中(您已从问题中的代码中省略)并查看 async/await 的文档。如果您对错误消息感到困惑,您稍后可能会陷入混乱。
  • 感谢丹尼尔·凯利。你回答了我的问题......我也应该使用异步方法:)。谢谢

标签: c# windows-phone-8 async-await flashlight


【解决方案1】:

你需要像这样改变你的方法:

public async Task Foo()
{
    //code
    try
        {
            MediaCapture mc = new MediaCapture();
            await mc.InitializeAsync();

            if (mc.VideoDeviceController.TorchControl.Supported == true)
            {
                mc.VideoDeviceController.TorchControl.Enabled = true;
                if (mc.VideoDeviceController.TorchControl.PowerSupported == true)
                {
                    mc.VideoDeviceController.TorchControl.PowerPercent = 100;
                }
            }
        }
        catch(Exception ex)
        {
            //TODO: Report exception to user
        }

}

【讨论】:

    猜你喜欢
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多