TextView Compound Drawable Mvvm交叉绑定
TextView Compound Drawable MvvmCross Binding
TextView
有一些 DrawableXXX
属性,如 DrawableStart
、DrawableEnd
、DrawableTop
等,以在 Drawable
旁边显示TextView
文字。是否可以使用 MvvmCross 绑定此 属性?我试过在我的绑定中使用 local:MvxBind="Drawablename 'check'"
但它没有显示 MvxBind 错误:
Failed to create target binding for binding DrawableName for check
如果此绑定未在 MvvmCross 中实现,那么我将如何自己完成此操作? N+28使用的方法是否仍然是添加自定义绑定的推荐方法?
目前没有任何 MvvmCross 定义用于添加复合可绘制对象的目标绑定。但是,您可以轻松添加自己的。如果您使用的是 MvvmCross 5+,则可以利用新类型的自定义绑定 类。 注意,下面的例子已经设置了drawable放在左边,但是你可以选择任意方向(或多个方向)放置.
public class CompoundDrawablesDrawableNameBinding : MvxAndroidTargetBinding<TextView, string>
{
public const string BindingIdentifier = "CompoundDrawableName";
public CompoundDrawablesDrawableNameBinding(TextView target) : base(target)
{
}
public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;
protected override void SetValueImpl(TextView target, string value)
{
var resources = AndroidGlobals.ApplicationContext.Resources;
var id = resources.GetIdentifier(value, "drawable", AndroidGlobals.ApplicationContext.PackageName);
if (id == 0)
{
MvxBindingTrace.Trace(MvxTraceLevel.Warning,
"Value '{0}' was not a known compound drawable name", value);
return;
}
target.SetCompoundDrawablesWithIntrinsicBounds(
left: id,
top: 0,
right: 0,
bottom: 0);
}
}
然后在您的 Setup.cs
中注册
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterCustomBindingFactory<TextView>(
CompoundDrawablesDrawableNameBinding.BindingIdentifier,
textView => new CompoundDrawablesDrawableNameBinding(textView));
}
然后在你的 XML 中你可以使用
local:MvxBind="CompoundDrawableName <<YOUR PROPERTY TO BIND TO>>"
TextView
有一些 DrawableXXX
属性,如 DrawableStart
、DrawableEnd
、DrawableTop
等,以在 Drawable
旁边显示TextView
文字。是否可以使用 MvvmCross 绑定此 属性?我试过在我的绑定中使用 local:MvxBind="Drawablename 'check'"
但它没有显示 MvxBind 错误:
Failed to create target binding for binding DrawableName for check
如果此绑定未在 MvvmCross 中实现,那么我将如何自己完成此操作? N+28使用的方法是否仍然是添加自定义绑定的推荐方法?
目前没有任何 MvvmCross 定义用于添加复合可绘制对象的目标绑定。但是,您可以轻松添加自己的。如果您使用的是 MvvmCross 5+,则可以利用新类型的自定义绑定 类。 注意,下面的例子已经设置了drawable放在左边,但是你可以选择任意方向(或多个方向)放置.
public class CompoundDrawablesDrawableNameBinding : MvxAndroidTargetBinding<TextView, string>
{
public const string BindingIdentifier = "CompoundDrawableName";
public CompoundDrawablesDrawableNameBinding(TextView target) : base(target)
{
}
public override MvxBindingMode DefaultMode => MvxBindingMode.OneWay;
protected override void SetValueImpl(TextView target, string value)
{
var resources = AndroidGlobals.ApplicationContext.Resources;
var id = resources.GetIdentifier(value, "drawable", AndroidGlobals.ApplicationContext.PackageName);
if (id == 0)
{
MvxBindingTrace.Trace(MvxTraceLevel.Warning,
"Value '{0}' was not a known compound drawable name", value);
return;
}
target.SetCompoundDrawablesWithIntrinsicBounds(
left: id,
top: 0,
right: 0,
bottom: 0);
}
}
然后在您的 Setup.cs
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterCustomBindingFactory<TextView>(
CompoundDrawablesDrawableNameBinding.BindingIdentifier,
textView => new CompoundDrawablesDrawableNameBinding(textView));
}
然后在你的 XML 中你可以使用
local:MvxBind="CompoundDrawableName <<YOUR PROPERTY TO BIND TO>>"