【问题标题】:StartPreviewAsync with CaptureElement in MVVM UWPStartPreviewAsync 与 MVVM UWP 中的 CaptureElement
【发布时间】:2017-10-25 21:23:38
【问题描述】:

我正在为 Prism 应用程序的 MVVM 相机工作,并使用此方法在 CaptureElement 组件中显示预览

private async Task StartPreviewAsync(string deviceId)
        {
            try
            {
                _mediaCapture = new MediaCapture();
                MediaCaptureInitializationSettings setting = new MediaCaptureInitializationSettings();
                setting.VideoDeviceId = deviceId;
                await _mediaCapture.InitializeAsync(setting);
                //_displayRequest.RequestActive();
                //DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
                CaptureElementSource = _mediaCapture;
                //This line throw exception
                await _mediaCapture.StartPreviewAsync();
            }
            catch (UnauthorizedAccessException)
            {
                Debug.WriteLine("The app was denied access to the camera");
            }
            catch (Exception ex)
            {
                Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message);
            }
        }

这是我的 CaptureElement.Source 道具:

public MediaCapture CaptureElementSource
        {
            get { return _captureElementSouce; }
            set { SetProperty(ref _captureElementSouce, value); }
        }

我得到了这个例外:

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Mateo.UILogic.ViewModels.CameraSelectionPageViewModel.<StartPreviewAsync>d__111.MoveNext()

请求的操作发生意外错误。预习 接收器未设置

任何人,请帮助我。

【问题讨论】:

  • 只是好奇,为什么你把MediaCapture放在你的视图模型中?我不认为MediaCapture 属于视图模型。
  • @Scavenger 我尝试了各种方法,但 MediaCapture 只是在它的 Dispatcher 中运行,它只存在于后面的代码中。所以我已经在后面的代码上写了我的代码。 :D 就像你说的,媒体捕获不属于虚拟机

标签: c# wpf mvvm camera uwp


【解决方案1】:

当我忘记检查 Package.appxManifst -> Capabilities 部分中的网络摄像头和麦克风选项时,我也遇到了同样的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 2019-12-08
    • 2017-09-02
    • 1970-01-01
    • 2017-05-14
    • 2016-04-09
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多