2

hope its something simple stupid, but I am spinning my wheels.

I have a Surface Pro 4, Windows 10 and using Visual Studio 2013 Professional.
Developing WPF using C# 4.5.

In summary, all I am trying to do a simple camera capture to save an image without resorting to other 3rd party libraries I have no control over. The rest of this post are details of other research findings that I HAVE looked into and tried working out and what has failed and what such message from the compiler.

Again, simple camera, capture picture, save to disk, but all the async-await options appear to throw compiler errors.

EDIT 1 SAMPLE CODE

Here is code from the WPF form. I do not even have any control in the form as I can not even get the 'await' to compile.

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

And my project also has per other links researched from below, the dlls for

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

Compile error for each of the 'await'

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

and I have "using System;" as the first line. Is there some other "await" going on when using WinRT vs default System include?

END OF EDIT

I started with https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.capture.cameracaptureui.aspx CameraCaptureUI class

I then found this link https://www.eternalcoding.com/?p=183 How to use specific WinRT API from Desktop apps I tried going through all the steps as outlined and getting errors associated with

await ... have a suitable GetAwaiter method.

So, I looked up about asynch and await and came across

How and When to use `async` and `await` how and when to use async and await.

So, I scrapped the first camera capture project, started a new, and did that version that has simple thread.sleep delay to show the concept of asynch / await. That part works.

So now, I add back the reference to the project and manually edit to add the

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

to expose the References expose the Windows / Core per the first link and then getting access to the Windows.Media, Windows.Storage. I add in just the first little bit of code about the CameraCaptureUI and CaptureFileAsync as just a starting point such as...

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

and get a compile error about:

'await' requires that the type 'Windows.Foundation.IAsyncOperation' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

Also, back to the original Windows version attempt via

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

I even have System.Runtime and System.RunTime.WindowsRuntime as references to the project but still fail on compile.

What am I missing. I know things change between different versions such as Windows 8.1 and Windows 10, and upgrades to features / libraries, but why await works one way, but not another.

Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142

1 Answers1

1

For those scratching their heads as I did, I finally came across another post that had a missing link...

How can i import windows.media.capture in my WPF project?

The difference was the

<TargetPlatformVersion>8.1</TargetPlatformVersion>

instead of 8.0.

8.1 showed many of the individual .dll references of Foundation, Media, etc but not the single "Windows" dll.

Once changed to 8.1 and recognized the WINDOWS dll, all worked with respect to the media manager or CameraCaptureUI options.

Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142