Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind source MediaCapture to CaptureElement using Caliburn.Micro?

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.

like image 348
Frederik Krautwald Avatar asked Dec 19 '22 14:12

Frederik Krautwald


1 Answers

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 :)

like image 71
Hawlett Avatar answered Dec 29 '22 10:12

Hawlett