mvvmcross videoview URL绑定 [英] mvvmcross videoview URL binding

查看:343
本文介绍了mvvmcross videoview URL绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVVMCross新的,目前有约束力的问题挣扎。
试图在Android上的VideoView组件的视频的URL绑定。

I am new in MVVMCross and currently struggling with a binding issue. Trying to bind the URL of an video with the VideoView component in android.


  • 有没有Mvx.MvxBind标记,可以自动完成呢?

  • 否则,我怎么能传递URL从MvxViewModel到MvxActivity?

如果第一个选项是不可能的,我会尝试获得URL和播放视频如下解释:的 http://developer.xamarin.com/recipes/android/media/video/play_video/

If the first option is not possible i will try to get the URL and play the video as explained here: http://developer.xamarin.com/recipes/android/media/video/play_video/

先谢谢了。

推荐答案

MvvmCross非常灵活。什么你问的是不是内置的东西,但你可以轻松地扩展MvvmCross增加对它的支持。

MvvmCross is very flexible. What you're asking for is not something that is built-in, but you can easily extend MvvmCross to add support for it.

MvvmCross有所谓的绑定建设者。这是你注册自定义目标绑定。有约束力的需要的类型,比如 VideoView 的和属性名状的 VIDEOURI的。

MvvmCross has what are called Binding Builders. This is where you register custom Target Bindings. A binding takes a type, like VideoView, and a property name like "VideoUri".

在MvvmCross看到像绑定属性:地方:MvxBind =VIDEOURI MyVideoUri将采取属性值从的 MyVideoUri 的,然后调用自定义绑定,这ulimiately将调用 videoView.SetVideoURI()

When MvvmCross sees a binding attribute like: local:MvxBind="VideoUri MyVideoUri" it will take the property value from MyVideoUri, then call the custom binding, which ulimiately will call videoView.SetVideoURI().

下面是步骤,你需要采取

Here are the steps you need to take.

1)在你的Andr​​oid项目,编辑的 Setup.cs 的,并添加下面,注册您的自定义绑定生成器。

1) In your Android project, edit Setup.cs and add the following, to register your custom binding builder.

    protected override MvxAndroidBindingBuilder CreateBindingBuilder()
    {
        return new MyAndroidBindingBuilder();
    }

2)创建的子类的默认的Andr​​oid绑定生成器自定义绑定生成器:

2) Create a custom binding builder that subclasses the default Android binding builder:

public class MyAndroidBindingBuilder : MvxAndroidBindingBuilder
{
    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        base.FillTargetFactories(registry);

        registry.RegisterCustomBindingFactory<VideoView>("VideoUri",
                                             videoView => new MvxVideoViewUriTargetBinding(videoView));

    }
}

3)创建一个自定义的目标类型VideoView和财产VIDEOURI绑定:

3) Create a custom target binding for type VideoView and property "VideoUri":

public class MvxVideoViewUriTargetBinding : MvxAndroidTargetBinding
{
    public MvxVideoViewUriTargetBinding(object target) : base(target)
    {
    }

    public override Type TargetType
    {
        get { return typeof (string); }
    }

    protected override void SetValueImpl(object target, object value)
    {
        var videoView = (VideoView) target;
        videoView.SetVideoURI(Uri.Parse((string)value));
    }
}

要在布局使用它,只需创建以下内容:

To use it in your layout, simply create the following:

1)在你的 layout.xml

<VideoView
    android:id="@+id/SampleVideoView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    local:MvxBind="VideoUri MyVideoUri" />

2)在你的视图模型的,增加对MyVidoeUri属性

2) In your ViewModel, add a property for MyVidoeUri

    public string MyVideoUri
    {
        get { return "http://ia600507.us.archive.org/25/items/Cartoontheater1930sAnd1950s1/PigsInAPolka1943.mp4"; }
    }

3)在你的视图的,你可以这样开始视频:

3) In you View, you can start the video like this:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.MyLayout);

        // SetVideoURI() already called via binding
        var videoView = FindViewById<VideoView>(Resource.Id.SampleVideoView);
        videoView.Start();
     }

看源MvxAndroidBindingBuilder查看当前绑定。
<一href=\"https://github.com/MvvmCross/MvvmCross/blob/bbf9a2ac76e74d9404f4b57036c6e29dfe2cc6c3/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs\" rel=\"nofollow\">https://github.com/MvvmCross/MvvmCross/blob/bbf9a2ac76e74d9404f4b57036c6e29dfe2cc6c3/Cirrious/Cirrious.MvvmCross.Binding.Droid/MvxAndroidBindingBuilder.cs

希望这有助于。

这篇关于mvvmcross videoview URL绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆