【问题标题】:How to bind to View's layout_weight in MvvmCross?如何在 MvvmCross 中绑定 View 的 layout_weight?
【发布时间】:2014-02-01 02:35:49
【问题描述】:

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

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:

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

【问题讨论】:

  • 据我所知,layout_weight 可以使用如下代码以编程方式设置:stackoverflow.com/questions/3224193/… - 因此您可以构建自定义绑定来支持这一点。但是,我不知道在布局已经显示之后是否可以轻松地动态更改权重值。
  • 布局显示后可以更改权重,我在java android中测试过,没问题。我在自定义绑定中使用了完全相同的代码,但它不起作用,看起来绑定本身存在问题。

标签: xamarin.android xamarin mvvmcross


【解决方案1】:

MvxSimplePropertyInfoTargetBindingFactory 只能用于真正的 C# 属性。

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

    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/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-28-CustomBinding/CustomBinding.Droid/Setup.cs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多