2013-10-30 42 views
1

我讀了很多解決方案在當前的問題,但沒有一次成功結合,我不能 弄清楚如何做到以下幾點:MVVM無法創建目標結合

轉換器

public class HexToUIColorValueConverter : MvxValueConverter<int, UIColor> 
{ 
    protected override UIColor Convert(int value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 

     return UIColor.FromRGB(
      (((float)((value & 0xFF0000) >> 16))/255.0f), 
      (((float)((value & 0xFF00) >> 8))/255.0f), 
      (((float)(value & 0xFF))/255.0f) 
     ); 
    } 
} 

結合

public SubtitleDetailViewCell(IntPtr handle) 
    : base(handle) 
{ 
    Initialize(); 

    this.DelayBind(() => 
    { 
     Accessory = UITableViewCellAccessory.DisclosureIndicator; 

     var set = this.CreateBindingSet<SubtitleDetailViewCell, ObservationMedicale>(); 
     set.Bind(MainLbl).To(observation => observation.Texte).WithConversion(new ByteArrayToStringValueConverter(), null); 
     set.Bind(LeftDetailLbl).To(observation => observation.SaisieLe).WithConversion(new StringFormatValueConverter(), "d MMM, HH:mm"); 
     set.Bind(RightDetailImgLbl.Label).SourceDescribed("PraticienNom + ' ' + PraticienPrenom"); 
     set.Bind(Label.Color).To(observation => observation.ObsCategorieCouleur).WithConversion(new HexToUIColorValueConverter(), null); 
     set.Apply(); 
    }); 
} 

綁定的對象

using System; 
using System.Drawing; 

using MonoTouch.CoreGraphics; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 

namespace Next.Client.Application.iOS.Views.UI 
{ 
    [Register("CellLabelView")] 
    public class CellLabelView : UIView 
    { 

     public UIColor Color { get; set; } 

     public CellLabelView() 
     { 
      Initialize(); 
     } 

     public CellLabelView(IntPtr handle) 
      : base(handle) 
     { 
     } 

     public CellLabelView(RectangleF bounds) 
      : base(bounds) 
     { 
      Initialize(); 
     } 

     void Initialize() 
     { 
      BackgroundColor = UIColor.Clear; 
      Opaque = false; 
     } 

     public override void Draw(RectangleF rect) 
     { 
      base.Draw(rect); 

      // get graphics context 
      using (CGContext gc = UIGraphics.GetCurrentContext()) 
      { 
       // set up drawing attributes 
       gc.SetLineWidth(1); 
       _color.SetFill(); 
       UIColor.Clear.SetStroke(); 

       //create geometry 
       var path = new CGPath(); 

       path.AddLines(new PointF[]{ 
        new PointF (0, 0), 
        new PointF (0, 8), 
        new PointF (4, 4), 
        new PointF (8, 8), 
        new PointF (8, 0) 
       }); 

       path.CloseSubpath(); 

       //add geometry to graphics context and draw it 
       gc.AddPath(path); 
       gc.DrawPath(CGPathDrawingMode.FillStroke); 
      } 
     } 
    } 
} 

我不明白它是如何失敗的顏色結合,對CellLabel的SET方法不會被調用,因此我在Draw()方法顏色是不是有效的對象。

回答

3

當前您正試圖直接綁定顏色對象 - 而不是嘗試綁定標籤上的顏色屬性。

嘗試:

set.Bind(Label).For(l => l.Color).To(observation => observation.ObsCategorieCouleur).WithConversion(new HexToUIColorValueConverter(), null); 

有關此流利的語法擊穿,包括For,看到https://github.com/MvvmCross/MvvmCross/wiki/Databinding#fluent


除此之外,你可能還應該嘗試在您Color自動屬性更改爲屬性裏面有一些代碼,每當顏色改變時,通過Invalidate請求重繪。

+0

工程就像一個魅力!非常感謝您的回答,我對c#和mvvmcross還是一個新鮮的東西,您的干預對我非常有幫助 – kitensei