MVVM binding CaptureElement

本文关键字:CaptureElement binding MVVM | 更新日期: 2023-09-27 18:02:49

我知道这个话题是重复的,但我尝试这个解决方案…(

好的,我在Windows Phone 8.1上编写应用程序。这个应用程序是QRCode扫描器在MVVM模式。我绑定CaptureElement有问题,因为Visual Studio返回错误:

WinRT information: Cannot deserialize XBF metadata property list as '%1' was not found in type '%0'.
好的,我的代码XAML:
<CaptureElement x:Name="Camera"   Height="300" Width="399.999969482422" Source={Binding ViewCapture}"></CaptureElement>

和,ViewModel

 private MediaCapture _viewCapture;
 public MediaCapture ViewCapture
        {
            get
            {
                return _viewCapture;
            }
            set
            {
                if (_viewCapture != value)
                {
                    _viewCapture = value;
                    NotifyPropertyChanged();
                }
            }
        }

谢谢你的帮助

MVVM binding CaptureElement

问题是由于CaptureElementSource属性引起的,其值为MediaCapture,但MediaCapture的预览尚未启动。

这两条指令必须依次执行:

captureElement.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();

我还没有找到一个优雅的解决方案,包括这两个指令在MVVM。我已经使用了一个ContentControl元素来动态地生成CaptureElement,在ViewModel内部。

视图:

<ContentControl x:Name="captureElement"
                Content="{Binding CaptureElement}"/>

ViewModel:

public class MainView : BaseView
{
    private CaptureElement _captureElement;
    public CaptureElement CaptureElement
    {
        get
        {
            return _captureElement;
        }
    }
    public MainView()
    {
        InitializeCamera();
    }
    private async void InitializeCamera()
    {
        // media capture creation
        _captureElement = new CaptureElement();
        _captureElement.Source = mediaCapture;
        await mediaCapture.StartPreviewAsync();
    }
}

MediaCapture应该包含在代表摄像头的服务中。我没有包括它,以保持解决方案简单。