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

后端 未结 4 1461
梦谈多话
梦谈多话 2021-02-10 22:03

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 MediaCa

4条回答
  •  被撕碎了的回忆
    2021-02-10 22:28

    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:

     
    

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

提交回复
热议问题