【发布时间】:2016-07-11 18:39:58
【问题描述】:
希望它是一些简单的愚蠢的东西,但我正在旋转我的车轮。
我有 Surface Pro 4、Windows 10 并使用 Visual Studio 2013 Professional。
使用 C# 4.5 开发 WPF。
总而言之,我只是想通过简单的相机捕捉来保存图像,而无需求助于我无法控制的其他 3rd 方库。这篇文章的其余部分详细介绍了我已经研究并尝试解决的其他研究结果,以及失败的原因以及编译器发出的此类消息。
同样,简单的相机、捕捉图片、保存到磁盘,但所有异步等待选项似乎都会引发编译器错误。
编辑 1 个示例代码
这是来自 WPF 表单的代码。我什至无法控制表单,因为我什至无法让“等待”编译。
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();
}
}
}
我的项目还具有从下面研究的其他链接,用于
的 dllSystem.Runtime.dll and
System.Runtime.WindowsRuntime.dll
每个“等待”的编译错误
Error 1 'await' requires that the type 'Windows.Foundation.IAsyncAction' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?
我有“使用系统;”作为第一行。使用 WinRT 与默认系统包含时是否还有其他“等待”?
编辑结束
我从https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.capture.cameracaptureui.aspx开始 CameraCaptureUI 类
然后我找到了这个链接https://www.eternalcoding.com/?p=183 如何使用桌面应用程序中的特定 WinRT API 我尝试完成概述的所有步骤并获得与
相关的错误await ... 有一个合适的 GetAwaiter 方法。
所以,我查了一下 asynch 和 await 并遇到了
How and When to use `async` and `await`
如何以及何时使用async 和await。
所以,我放弃了第一个相机捕捉项目,开始了一个新项目,并做了那个版本 它具有简单的 thread.sleep 延迟来显示 asynch / await 的概念。这部分有效。
所以现在,我添加回对项目的引用并手动编辑以添加
<PropertyGroup>
<TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>
要公开引用,请根据第一个链接公开 Windows/Core,然后访问 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 方法。 您是否缺少“系统”的使用指令?
另外,尝试通过返回原始 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 以一种方式工作,而不是另一种方式。
【问题讨论】:
-
不清楚什么问题背景资料太多
标签: c# wpf camera async-await