2016-03-21 77 views
3

我正試圖綁定Android中TextView的文本顏色。這裏是我的(截)XAML:如何綁定TextView的TextColor?

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:MvxBind=" TextColor CategoryTextColor(Category)"/> 

其中CategoryTextColorValueConverter如下:

public class CategoryTextColorConverter : MvxValueConverter<ShowCategory, Color> 
{ 
    protected override Color Convert (ShowCategory value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == ShowCategory.AllShows) 
     { 
      return Color.Blue; 
     } 
     return Color.Red; 
    } 
} 

轉換器獲取調用和按預期返回一個顏色,但文本顏色從不TextView的變化。我有一個類似的工作正常的背景顏色綁定。我在這裏看到In MvvmCross how do I do custom bind properties,也許我需要創建一個自定義綁定,但我找不到MvxBaseAndroidTargetBinding。也許我需要從nuget安裝一個單獨的包?

回答

7

你必須做的唯一的事情,正在安裝MvvMCross顏色插件,因爲TextColor與它來。它不包含在Core中。您發佈的解決方案有效

參見:https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins#color

提示:你不必編寫特定於平臺的ValueConvertes,如果你使用MvxColorValueConverter<ShowCategory>,你可以在不同的平臺上共享。

public class CategoryTextColorConverter : MvxColorValueConverter<ShowCategory> 
{ 
    protected override MvxColor Convert(ShowCategory value, object parameter, CultureInfo culture) 
    { 
     if (value == ShowCategory.AllShows) 
     { 
      return MvxColors.Blue; 
     } 
     return MvxColors.Red; 
    } 
} 
+0

我正在使用這個,文本本身現在不可見。我在轉換方法中保留了一個斷點,它不會在那裏出現。 – GvSharma

2

MVVMCross綁定通過綁定到對象的屬性來工作,而android:textColor在Android XML中正常工作時,它是對象的基礎方法SetTextColor的快捷方式,無法直接綁定到SetTextColor。你可以做的是創建一個擴展TextView並具有可綁定TextColor屬性的類,然後在你的Android XML中使用它並綁定到該類。例如:

public class ExtendedTextView : TextView 
{ 
    public ExtendedTextView(Context context): base (context) { } 
    public ExtendedTextView(Context context, IAttributeSet attrs) : base (context, attrs) { } 
    public ExtendedTextView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { } 
    public ExtendedTextView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base (context, attrs, defStyleAttr, defStyleRes) { } 
    protected ExtendedTextView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { } 

    public Color BindableTextColor // property to bind to in XML 
    { 
     get { return new Color(CurrentTextColor); } 
     set { SetTextColor(value);} 
    } 
} 
+0

這看起來應該可以工作,但顏色永遠不會改變,儘管BindableTextColor的setter按照新顏色值的預期調用。 –

1

感謝您的輸入所有 - 最好我會用@斯文邁克爾的跨平臺的解決方案,但我最終實現定製Android中結合。

我的代碼如下。請注意0​​現在稱爲MvxAndroidTargetBinding

public class TextColorBinding : MvxAndroidTargetBinding { 

    private readonly TextView _textView; 

    public TextColorBinding(TextView textView) : base(textView) 
    { 

     _textView = textView; 
    } 

    public static void Register(IMvxTargetBindingFactoryRegistry registry) { 
     registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("TextColor", (textView) => new TextColorBinding(textView))); 
    } 

    #region implemented abstract members of MvxTargetBinding 

    public override Type TargetType 
    { 
     get { return typeof(Color); } 
    } 

    #endregion 

    #region implemented abstract members of MvxConvertingTargetBinding 

    protected override void SetValueImpl (object target, object value) 
    { 
     var color = (Color)value; 

     if (color != null) { 

      var textView = (TextView)target; 

      try { 
       textView.SetTextColor (color); 
      } catch (Exception ex) { 
       MvxTrace.Error (ex.ToLongString()); 
       throw; 
      } 

     } else { 
      MvxBindingTrace.Trace (MvxTraceLevel.Warning, "Value was not a valid Color"); 
     } 
    } 
    #endregion 
} 

不要忘記註冊您的Setup.cs結合如下太:

protected override void FillTargetFactories(MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry) 
    { 
     TextColorBinding.Register (registry); 
     base.FillTargetFactories(registry); 
    } 
+2

這是「完全」mvvmcross顏色插件的作用^^ https://github.com/MvvmCross/MvvmCross-Plugins/blob/master/Color/MvvmCross.Plugins.Color.Droid/BindingTargets/MvxDefaultColorBindingSet.cs –

+0

雖然這解決方案確實起作用,我從我的項目中刪除了這段代碼,並安裝了MvvmCross Color插件,該插件也非常完美。謝謝@ Sven-MichaelStübe - 總是從項目中刪除不必要的代碼:) –

+1

是的,這是理解MvvMCross如何綁定的好習慣。你現在至少可以用可綁定的屬性來喚醒自定義視圖:) #achivementunlocked –

0

爲此,色彩調整及其他用途可以使用下面的代碼示例:

Android.Graphics.Color color = new Android.Graphics.Color(element.CurrentTextColor);