c# - 如何在没有模式的情况下将源 MediaCapture 绑定(bind)到 CaptureElement?

标签 c# mvvm windows-phone-8.1

我在 Windows Phone 8.1 上编写应用程序。我正在使用没有模式的 MVVM。
我无法将 MediaCapture 对象绑定(bind)到 View 中的 CaptureElement。
我知道这个话题重复了,但我尝试了这个解决方案

我的代码 XAML:

<ContentControl HorizontalAlignment="Left"         
Width="320" Height="140" Content="{Binding CaptureElement}"Margin="40,183,0,257"/>   

在我的 View 模型中:
private CaptureElement _captureElement;
public CaptureElement CaptureElement
{
    get
    {
        return _captureElement;
    }

    set 
    { 
        _captureElement = value; OnChange("CaptureElement"); }
    }  
}

最佳答案

由于您的代码不完整。我不确定你真正遇到了哪个问题。或者你只是不知道怎么写。我这里有一个完整的解决方案,可以满足您的要求。我已经测试过了。

XAML 代码

    <ContentControl HorizontalAlignment="Left"  Width="320" Height="140" Content="{Binding CaptureElement}" Margin="40,183,0,257"/> 

MainPage() 中的代码
MyViewModel view = new MyViewModel(); this.DataContext = view;         

在查看模式
class MyViewModel: NotificationBase
{
    private MediaCapture _mediaCapture;
    public MediaCapture MediaCapture
    {
        get
        {
            if (_mediaCapture == null) _mediaCapture = new MediaCapture();
            return _mediaCapture;
        }
        set
        { 
            _mediaCapture = value;
        }
    }
    private CaptureElement _captureElement;
    public CaptureElement CaptureElement
    {
        get
        {
            if (_captureElement == null) _captureElement = new CaptureElement();
            return _captureElement;
        }
        set
        {               
            _captureElement = value;
        }
    }

    public MyViewModel()
    {
        ConfigureMedia();
    }

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

}

同时上传这个演示here ,您可以下载进行测试。

关于c# - 如何在没有模式的情况下将源 MediaCapture 绑定(bind)到 CaptureElement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35994644/

相关文章:

wpf - Windows Phone 8.1,如何绑定(bind)样式到 View ?

c# - 快速应用恢复不工作

c# - Windows Phone 8.1 检查密码是否设置否则加载新页面

c# - 异步(非阻塞)代码的可伸缩性优势是什么?

c# - 在 WPF (MVVM) 中动态更改窗口的用户控件

wpf - 绑定(bind)到字符串列表时的列表框项目的工具提示

javascript - MVVM 在 DHTML RIA 应用程序(无 Silverlight/WPF)中是否可能/可行?

c# - 如何从对 web 服务的请求中获取客户端 IP 地址

c# - System.Environment.OSVersion .NET Core 5.0 Framework 替代品?

c# - 输入箭头键时如何防止MDI子窗体发生变化?