我想在Android中使用If-Else ValueCombiner绑定TextView的TextStyle属性。我尝试了以下操作,但创建绑定失败:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_gravity="center_vertical" android:layout_row="0" android:layout_column="1" android:textSize="28dp" android:gravity="left" android:text="MyText" local:MvxBind="TextStyle If(ShowBold, 'bold', 'normal')" />
我用Text属性测试了类似的绑定,它工作得很好,所以我猜它正在寻找字符串以外的其他东西?
发布于 2015-08-12 14:51:35
有点晚了,但我有同样的要求,现在就做了。
在您的设置文件中添加以下内容(我有两个自定义绑定属性,样式和汇总):
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("Style", textView => new StyleTextViewBinding(textView)));
registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("Summary", textView => new SummaryTextViewBinding(textView)));
}在我的TextView中(我的自定义绑定显然是样式的,Text和TextColor是转换器):
<TextView
style="@style/TeamDifficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/dummy_title"
local:MvxBind="Text TeamDifficultyText(RowItem.DifficultyEnumCaptain1Int); TextColor TeamDifficultyTextColor(RowItem.DifficultyEnumCaptain1); Style RowItem.DifficultyEnumCaptain1;" />和实际的代码(基本上它检查我的文本是否为空,如果是,它将加粗,因为我的转换器会在后面添加一个值):
public class StyleTextViewBinding : MvxAndroidTargetBinding
{
readonly TextView _textView;
public StyleTextViewBinding(TextView textView) : base(textView)
{
_textView = textView;
}
#region implemented abstract members of MvxConvertingTargetBinding
protected override void SetValueImpl(object target, object value)
{
_textView.SetTypeface(_textView.Typeface, Android.Graphics.TypefaceStyle.Bold);
if (value != null && Convert.ToBoolean(value))
_textView.SetTypeface(_textView.Typeface, Android.Graphics.TypefaceStyle.Normal);
}
#endregion
public override Type TargetType
{
get { return typeof(bool); }
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
}希望这能有所帮助!
发布于 2014-12-21 01:11:08
下面是Stuart帮助别人完成的一个文本颜色示例。In MvvmCross how do I do custom bind properties
使用这一点,您应该能够对文本样式执行反向工程。
https://stackoverflow.com/questions/27572586
复制相似问题