首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用MVVMCross绑定TextStyle?

如何使用MVVMCross绑定TextStyle?
EN

Stack Overflow用户
提问于 2014-12-20 03:21:00
回答 2查看 1.6K关注 0票数 3

我想在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属性测试了类似的绑定,它工作得很好,所以我猜它正在寻找字符串以外的其他东西?

EN

回答 2

Stack Overflow用户

发布于 2015-08-12 14:51:35

有点晚了,但我有同样的要求,现在就做了。

在您的设置文件中添加以下内容(我有两个自定义绑定属性,样式汇总):

代码语言:javascript
复制
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中(我的自定义绑定显然是样式的TextTextColor是转换器):

代码语言:javascript
复制
<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;" />

和实际的代码(基本上它检查我的文本是否为空,如果是,它将加粗,因为我的转换器会在后面添加一个值):

代码语言:javascript
复制
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; }
    }
}

希望这能有所帮助!

票数 2
EN

Stack Overflow用户

发布于 2014-12-21 01:11:08

下面是Stuart帮助别人完成的一个文本颜色示例。In MvvmCross how do I do custom bind properties

使用这一点,您应该能够对文本样式执行反向工程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27572586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档