在我的Xamarin Android应用程序中,我尝试在布尔值上使用转换器来突出显示列表中的选定项目。
有没有人知道甚至可以在布尔值上使用转换器来选择一个或另一个drawable作为LinearLayout的背景?
我觉得我错过了什么。我尝试从转换器中返回各种类型,但似乎没有任何效果。
yPosition = 120
答案 0 :(得分:2)
答案 1 :(得分:2)
我做了类似的事情,我有一个int(RowItem.SummaryEnumPlayersInt)我将其转换为drawable:
<TextView
style="@style/TeamDifficulty"
android:layout_width="15dp"
android:layout_height="15dp"
android:gravity="center"
android:background="@drawable/background_circle"
local:MvxBind="Text RowItem.SummaryEnumPlayersInt; Summary RowItem.SummaryEnumPlayers" />
注意自定义&#34;摘要&#34;绑定,这是魔术发生的地方,在setup:
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("Summary", textView => new SummaryTextViewBinding(textView)));
}
这是自定义绑定类:
public class SummaryTextViewBinding : MvxAndroidTargetBinding
{
readonly TextView _textView;
SummaryEnumeration _currentValue;
public SummaryTextViewBinding(TextView textView) : base(textView)
{
_textView = textView;
}
#region implemented abstract members of MvxConvertingTargetBinding
protected override void SetValueImpl(object target, object value)
{
if (!string.IsNullOrEmpty(_textView.Text))
{
_currentValue = (SummaryEnumeration)Convert.ToInt32(_textView.Text);
SetTextViewBackground();
}
}
#endregion
void SetTextViewBackground()
{
switch (_currentValue)
{
case SummaryEnumeration.Easy:
_textView.SetBackgroundResource(Resource.Drawable.background_circle_green);
_textView.Text = string.Empty;
break;
case SummaryEnumeration.Medium:
_textView.SetBackgroundResource(Resource.Drawable.background_circle_yellow);
_textView.Text = string.Empty;
break;
case SummaryEnumeration.Difficult:
_textView.SetBackgroundResource(Resource.Drawable.background_circle_red);
_textView.Text = string.Empty;
break;
case SummaryEnumeration.None:
_textView.SetBackgroundResource(Resource.Drawable.background_circle_none);
_textView.Text = LocalizationConstants.Nothing;
break;
}
}
public override Type TargetType
{
get { return typeof(bool); }
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
}
享受!