如何在 MvvmCross 中绑定到 View 的 layout_weight? [英] How to bind to View's layout_weight in MvvmCross?
问题描述
绑定到 View(或任何其他 Android 控件)权重的最简单方法是什么?因为这个属性没有setter,我试过自定义绑定,但是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:Warning: 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
[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);
}
这篇关于如何在 MvvmCross 中绑定到 View 的 layout_weight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!