如何在MvvmCross中绑定到View的layout_weight? [英] How to bind to View's layout_weight in MvvmCross?

查看:58
本文介绍了如何在MvvmCross中绑定到View的layout_weight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绑定到View(或任何其他Android控件)权重的最简单方法是什么?因为此属性没有设置器,所以我尝试了自定义绑定,但是id似乎不起作用:

What is the easiest way to bind to View's (or any other Android control) weight? Because this property doesn't have a setter, I tried custom binding, but id doesn't seem to work:

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

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

    protected override void SetValueImpl(object target, object value)
    {
        var realTarget = target as View;
        if(target == null)
            return;

        ViewGroup.LayoutParams layoutParameters = realTarget.LayoutParameters;
        realTarget.LayoutParameters = new LinearLayout.LayoutParams(layoutParameters.Width, layoutParameters.Height,
                                                                  (int) value);
    }
}

设置中的注册:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
    registry.RegisterFactory(new MvxSimplePropertyInfoTargetBindingFactory(typeof(ViewWeightCustomBinding), typeof(View), "ViewWeight"));
    base.FillTargetFactories(registry);
}

和.axml

 <View
        android:layout_width="0dp"
        android:layout_height="3dp"
        android:background="@color/green_holo"
        local:MvxBind="ViewWeight Id" />

我可以在调试窗口中看到Waring:

I can see Waring in debug window:

[0:] MvxBind:警告:5.20无法创建用于绑定ID的ViewWeight的目标绑定 [0:] MvxBind:警告:5.20无法创建用于绑定ID的ViewWeight的目标绑定 01-31 10:54:57.247 I/mono-stdout(3795):MvxBind:警告:5.20无法创建用于绑定ID的ViewWeight的目标绑定

[0:] MvxBind:Warning: 5.20 Failed to create target binding for binding ViewWeight for Id [0:] MvxBind:Warning: 5.20 Failed to create target binding for binding ViewWeight for Id 01-31 10:54:57.247 I/mono-stdout( 3795): MvxBind:Warning: 5.20 Failed to create target binding for binding ViewWeight for Id

推荐答案

MvxSimplePropertyInfoTargetBindingFactory仅可用于真正的C#属性.

MvxSimplePropertyInfoTargetBindingFactory can only be used for real C# properties.

对于发明的伪"属性,您需要使用自定义绑定注册,如n = 28教程中所示-

For invented "pseudo" properties, you need to use a custom binding registration like that shown in the n=28 tutorial -

    protected override void FillTargetFactories(Cirrious.MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
    {
        registry.RegisterCustomBindingFactory<BinaryEdit>(
                        "N28", 
                        binary => new BinaryEditFooTargetBinding(binary) );
        base.FillTargetFactories(registry);
    }

https://github.com. com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-28-CustomBinding/CustomBinding.Droid/Setup.cs

这篇关于如何在MvvmCross中绑定到View的layout_weight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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