Question

I've read a lot of solutions on the current problem but none of them worked and I cannot figure out how to do the following:

the converter

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)
        );
    }
}

the binding

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();
    });
}

the binded object

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);
            }
        }
    }
}

I don't understand how it fails to bind the color, the SET method on CellLabel is never called and so my Color in the Draw() method isn't a valid object.

Was it helpful?

Solution

Currently you are trying to bind the color object directly - rather than trying to bind the color property on your label.

Try:

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

For a breakdown on this fluent syntax, including For, see https://github.com/MvvmCross/MvvmCross/wiki/Databinding#fluent


Beyond this, you probably also should try to change your Color auto-property to a property with some code inside that requests a redraw via Invalidate whenever the color is changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top