如何绑定到MvvmCross中查看的Layout_Weight? [英] How to bind to View's layout_weight in MvvmCross?
本文介绍了如何绑定到MvvmCross中查看的Layout_Weight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
绑定到View(或任何其他Android控件)权重的最简单方法是什么?因为此属性没有setter,所以我尝试了自定义绑定,但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" />
我可以在调试窗口中看到警告:
[0:] MvxBind:警告:5.20无法为ID的绑定View Weight创建目标绑定 [0:]MvxBind:警告:5.20无法为ID的绑定视图创建目标绑定 01-31 10:54:57.247 i/mono-stdout(3795):MvxBind:Warning:5.20无法为ID的绑定视图创建目标绑定
推荐答案
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);
}
这篇关于如何绑定到MvvmCross中查看的Layout_Weight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文