用Windows拍照 10、WinRT

Take picture with Windows 10, WinRT

希望它是一些简单的愚蠢的东西,但我正在旋转我的轮子。

我有 Surface Pro 4、Windows 10 并使用 Visual Studio 2013 Professional。
使用 C# 4.5 开发 WPF。

总而言之,我只是想做一个简单的相机捕捉来保存图像,而不求助于我无法控制的其他第 3 方库。 post 的其余部分是我调查并尝试解决的其他研究结果的详细信息,以及失败的原因以及来自编译器的此类消息。

同样,简单的相机,捕获图片,保存到磁盘,但所有异步等待选项似乎都会引发编译器错误。

编辑 1 个示例代码

这是 WPF 表单中的代码。我什至无法控制表单,因为我什至无法编译 'await'。

using System;
using Windows.Media.Capture;
using System.Windows;

namespace CameraCapture
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            JustDoIt();
        }

        private MediaCapture _mediaManager;
        private async void JustDoIt()
        {
            //initialize mediacapture with default camera
            _mediaManager = new MediaCapture();
            await _mediaManager.InitializeAsync();

            if (_mediaManager == null)
                MessageBox.Show("Failed Initialize");

            await _mediaManager.StartPreviewAsync();
        }
    }
}

而且我的项目还有其他 link 从下面研究过的,

的 dll
System.Runtime.dll   and
System.Runtime.WindowsRuntime.dll

每个 'await'

的编译错误
Error   1   'await' requires that the type 'Windows.Foundation.IAsyncAction' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

我将 "using System;" 作为第一行。使用 WinRT 与默认系统包含时是否还有其他 "await" 发生?

编辑结束

我从 https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.capture.cameracaptureui.aspx 开始 CameraCaptureUI class

然后我找到了这个 link https://www.eternalcoding.com/?p=183 如何从桌面应用程序使用特定的 WinRT API 我尝试按照概述完成所有步骤并收到与

相关的错误

await ...有合适的GetAwaiter方法。

所以,我查找了有关 asynch 和 await 的信息,然后遇到了

How and When to use `async` and `await` 如何以及何时使用 asyncawait.

所以,我放弃了第一个相机捕捉项目,开始了一个新的,并做了那个版本 它有简单的 thread.sleep 延迟来显示异步/等待的概念。那部分有效。

所以现在,我将引用添加回项目并手动编辑以添加

  <PropertyGroup>
    <TargetPlatformVersion>8.0</TargetPlatformVersion>
  </PropertyGroup>

公开引用,首先公开 Windows / 核心 link,然后访问 Windows.Media、Windows.Storage。我添加了关于 CameraCaptureUI 和 CaptureFileAsync 的第一小段代码作为起点,例如...

private async void Camera1()
{
    var cameraUi = new Windows.Media.Capture.CameraCaptureUI();
    var capturedMedia = await cameraUi.CaptureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.Video);

    if (capturedMedia == null)
        return;

    MessageBox.Show("Valid Camera");
}

并得到一个编译错误:

'await' 要求类型 'Windows.Foundation.IAsyncOperation' 有合适的 GetAwaiter 方法。 您是否缺少 'System' 的 using 指令?

另外,通过

回到原来的Windows版本尝试
private async void Camera2()
{
    CameraCaptureUI dialog = new CameraCaptureUI();
    Windows.Foundation.Size aspectRatio = new Windows.Foundation.Size(16, 9);
    dialog.PhotoSettings.CroppedAspectRatio = aspectRatio;

    StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file == null)
        return;

    MessageBox.Show("Valid Storage File Captured");
}

我什至有 System.Runtime 和 System.RunTime.WindowsRuntime 作为项目的参考,但仍然编译失败。

我错过了什么。我知道 Windows 8.1 和 Windows 10 等不同版本之间的情况会发生变化,并且会升级到功能/库,但为什么 await 以一种方式工作,而不是以另一种方式工作。

对于那些像我一样摸不着头脑的人,我终于找到了另一个 post,它缺少 link...

区别在于

<TargetPlatformVersion>8.1</TargetPlatformVersion>

而不是 8.0。

8.1 显示了 Foundation、Media 等的许多单独的 .dll 引用,但没有显示单个 "Windows" dll。

一旦更改为 8.1 并识别 WINDOWS dll,所有关于媒体管理器或 CameraCaptureUI 选项的工作。