4

On Windows Phone 8.1, I am using the Caliburn.Micro view-model-first approach, but as the view model cannot have any knowledge of the view, I cannot see how I can bind a MediaCapture object to a CaptureElement in the view.

5
  • They are both frameworkelements so they both can be bound, and to be clear you are talking about CaptureElement and MediaElement
    – mvermef
    May 29, 2014 at 23:19
  • <MediaElement Source="{Binding SomeCaptureElement}" />
    – mvermef
    May 29, 2014 at 23:30
  • No, I am talking about the MediaCapture object. In MVVM context, the CaptureElement lives in the View, while the MediaCapture object lives in the ViewModel. As the ViewModel should be completely agnostic about the View, how can the MediaCapture be bound as source? May 30, 2014 at 7:22
  • MediaCapture it is the action to the event... is it not?
    – mvermef
    May 31, 2014 at 4:04
  • MediaCapture is the class that gets instantiated to capture photos, audio, and video from the capture device. May 31, 2014 at 9:01

4 Answers 4

8

I had the same problem. I'm using MVVM Light with Windows Phone 8.1 WinRT (Universal Apps).

I used ContentControl and binded to CaptureElement:

 <ContentControl HorizontalAlignment="Left"         
                Width="320" Height="140" Content="{Binding CaptureElement}"/>

CaptureElement and MediaCapture are properties in my ViewModel:

private MediaCapture _mediaCapture;
        public MediaCapture MediaCapture
        {
            get
            {
                if (_mediaCapture == null) _mediaCapture = new MediaCapture();
                return _mediaCapture;
            }
            set
            {
                Set(() => MediaCapture, ref _mediaCapture, value);
            }
        }
        private CaptureElement _captureElement;
        public CaptureElement CaptureElement
        {
            get
            {
                if (_captureElement == null) _captureElement = new CaptureElement();
                return _captureElement;
            }
            set
            {
                Set(() => CaptureElement, ref _captureElement, value);
            }
        }

And next I call ConfigureMedia() in ViewModel's constructor:

   async void ConfigureMedia()
    { 
        await MediaCapture.InitializeAsync();
        CaptureElement.Source = MediaCapture;
        await MediaCapture.StartPreviewAsync();
    }

It's important to firstly initialize MediaCapture, next set Source and finally StartPeview. For me it works :)

2
  • My project was dropped long ago, so I cannot confirm if this would solve my issue. However, I will accept this as the answer. May 26, 2015 at 22:45
  • Be aware, this may potentially lead to memory leak, because CaptureElement inherits from Controls, and using Controls in ViewModel (outside of viewtree) is not a good idea, as Control's finalizer may not be called. Jun 10, 2015 at 10:21
1

If you're trying to keep strict view / view model separation then there are couple of possibilities.

Have you tried straight binding?

<CaptureElement Source="{Binding SomeMediaCapture}" />

If that doesn't work another one is create your own attached property that you could put on CaptureElement. When that property is set you can set the source yourself.

<CaptureElement custom:CaptureHelper.Source="{Binding SomeMediaCapture}" />

Here's a sample of doing some similar with web view and creating an html binding.

The way I tend to do this though is create an interface abstracting the view (say ICaptureView) that the view implements.

I can then cast the view held by the view model

var captureView = (ICaptureView) GetView();

where ICaptureView implements a SetCaptureSource method. This way it's still testable as you can attach a mock ICaptureView to the view model for testing.

4
  • 1
    Directly binding the MediaCapture as source for CaptureElement is not possible as the MediaCapture requires to be initialized first. I have been doing exactly what you are proposing by creating a utility/helper class that creates a dependency property which registers a property by type of MediaCapture. However, this results in a very peculiar error when run: WinRT information: Cannot deserialize XBF metadata property list as '%1' was not found in type '%0'. It is similar to the bug reported here. May 31, 2014 at 11:15
  • Could you initialise internally before setting to the bound property? Otherwise I'd go with the third option that I know works. Jun 1, 2014 at 21:25
  • @FrederikKrautwald Did you make progress on this? I'm facing the exact same issue. Sep 2, 2014 at 14:13
  • @MarkusBruckner Not any progress that solved the issue. I came to realize that either Microsoft or Caliburn has a problem, and I believe it’s Microsoft, which won’t let itself solve through a ViewModel-first approach. So the project got dropped. Sep 3, 2014 at 23:23
1

Adding to Hawlett's answer, I had to do a little bit more to get the camera displaying correctly. I have changed ConfigureMedia() to be:

private async void ConfigureMedia()
{
    _deviceInformationCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    await MediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
    {
        VideoDeviceId = _deviceInformationCollection[_deviceInformationCollection.Count - 1].Id
        // The rear-facing camera is the last in the list
    });
    MediaCapture.VideoDeviceController.PrimaryUse = CaptureUse.Photo;
    MediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
    CaptureElement.Source = MediaCapture;
    CaptureElement.Stretch = Stretch.UniformToFill;
    await MediaCapture.StartPreviewAsync();
}
1

I used ContentControl and bound to CaptureElement and It works for me but only the first time. If I navigate to another page and I come back to camera's page I can't see camera preview. I don't call method as StopPreviewAsync() I only navigate to another page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.