2014-01-31 22 views
0

綁定View的(或任何其他Android控件)權重的最簡單方法是什麼?因爲這個屬性沒有一個二傳手,我想自定義綁定,但ID似乎不工作:在設置如何綁定到視圖的MvvmCross中的layout_weight?

public class ViewWeightCustomBinding : MvxAndroidTargetBinding 
{ 
    public ViewWeightCustomBinding(object target) : base(target) 
    { 
    } 

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

    protected override void SetValueImpl(object target, object value) 
    { 
     var realTarget = target as View; 
     if(target == null) 
      return; 

     ViewGroup.LayoutParams layoutParameters = realTarget.LayoutParameters; 
     realTarget.LayoutParameters = new LinearLayout.LayoutParams(layoutParameters.Width, layoutParameters.Height, 
                    (int) value); 
    } 
} 

登記:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry) 
{ 
    registry.RegisterFactory(new MvxSimplePropertyInfoTargetBindingFactory(typeof(ViewWeightCustomBinding), typeof(View), "ViewWeight")); 
    base.FillTargetFactories(registry); 
} 

而且.axml

<View 
     android:layout_width="0dp" 
     android:layout_height="3dp" 
     android:background="@color/green_holo" 
     local:MvxBind="ViewWeight Id" /> 

我可以在調試窗口看到Waring:

[0:] MvxBind:Warning:5.20無法爲綁定ViewWeight創建Id的目標綁定 [0:] MvxBind:Warning:5.20無法爲綁定ViewWeight創建目標綁定Id 01-31 10:54:57.247 I/mono-stdout (3795):MvxBind:警告:5.20未能創建目標結合ViewWeight Id的

+0

據我所知'layout_weight'可使用以下代碼編程式設置:http://stackoverflow.com/questions/3224193/set-the-layout-weight-of-a-textview-programmatically - 所以你可以建立一個自定義綁定來支持這個。但是,我不知道佈局已經顯示後是否可以輕鬆更改權重值。 – Stuart

+0

這是可以改變佈局後顯示的重量,我測試它在Java Android和它是好的。我在自定義綁定中使用了完全相同的代碼,但它不起作用,看起來像綁定本身存在問題。 – user963935

回答

1

MvxSimplePropertyInfoTargetBindingFactory只能用於實時C#屬性結合。

對於發明了「僞」的屬性,則需要使用自定義綁定註冊一樣,在n = 28教程中所示 -

protected override void FillTargetFactories(Cirrious.MvvmCross.Binding.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry) 
    { 
     registry.RegisterCustomBindingFactory<BinaryEdit>(
         "N28", 
         binary => new BinaryEditFooTargetBinding(binary)); 
     base.FillTargetFactories(registry); 
    } 

https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-28-CustomBinding/CustomBinding.Droid/Setup.cs

相關問題