mvvm无法为绑定创建目标绑定

时间:2013-10-30 16:50:23

标签: data-binding mvvmcross uicolor

我已经阅读了很多关于当前问题的解决方案但是没有一个能够工作,我不能 弄清楚如何执行以下操作:

转换器

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()方法中的Color不是有效对象。

1 个答案:

答案 0 :(得分: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重绘。