是否可以使用MvvmCross将对象的高度绑定到viewmodel中的属性?
<ImageView
android:src="@drawable/blue_cat_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
local:MvxBind="layout_height User.Height; layout_width User.Width" />
Xamarin工作室抱怨如果没有定义layout_height或layout_width,所以我希望有办法通过绑定来覆盖属性。
我有五个图标,我只希望其中一个图标基于VM的属性更大,我还假设我将使用值转换器。
答案 0 :(得分:4)
我认为采取这种方法的方法与How to bind to View's layout_weight in MvvmCross?
中的方法类似public class Scaling
{
public double Height {get; private set;}
public double Width {get; private set;}
// ctor
}
public Scaling CurrentScaling
{
get { return _scaling; }
set { _scaling = value; RaisePropertyChanged(() => CurrentScaling); }
}
public class ViewScalingCustomBinding : MvxAndroidTargetBinding
{
public ViewScalingCustomBinding(object target) : base(target)
{
}
public override Type TargetType
{
get { return typeof (Scaling); }
}
protected override void SetValueImpl(object target, object value)
{
var realTarget = target as View;
if(target == null)
return;
var scaling = value as Scaling;
ViewGroup.LayoutParams layoutParameters = realTarget.LayoutParameters;
realTarget.LayoutParameters = new LinearLayout.LayoutParams(scaling.Width, scaling.Height);
}
}
protected override void FillTargetFactories(Cirrious.MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterCustomBindingFactory<View>(
"ScaleMe",
v => new ViewScalingCustomBinding(v) );
base.FillTargetFactories(registry);
}
local:MvxBind="ScaleMe CurrentScaling"
注意:
LayoutParameters
的类型取决于容器 - 在上面的代码中,我使用了LinearLayout
View
的{{1}}和ScaleX
属性 - 这就是我更喜欢使用的内容。