2
votes

I am trying to create a custom control . Below is my code

public class BoxControlRenderer:ViewRenderer<BoxControl,BoxControlView>
    {
        BoxControlView boxControlView;

        protected override void OnElementChanged (ElementChangedEventArgs<BoxControlView> e)
        {
            base.OnElementChanged (e);
            this.lineControlView = new BoxControlView (this.Bounds);
            this.SetNativeControl(this.boxControlView); 
        }


        protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged (sender, e);

            if (e.PropertyName == BoxControlView.BoxControlProperty.PropertyName)
            {
                this.boxControlView.BoxControlProperty = this.Element.CheckedValue;
            }
        }

    }

My BoxControlView View:

[Register("BoxControlView")]
    public class BoxControlView :UIView
    {
        UIImageView imageView;

        BoxControl _control;

        [Export("initWithFrame:")]
        public BoxControlView(CGRect bounds)
            : base(bounds)
        {
            imageView = new UIImageView (new CGRect (0, 0, 40, 18));
        }

        private bool _boxControlProperty;

        public bool BoxControlProperty
        {
            get {
                return _boxControlProperty;
                }
            set{ 
                value = _boxControlProperty;
                OnPropertyChanged ("BoxControlProperty");
                SetNeedsDisplay ();

            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler (this, new PropertyChangedEventArgs (propertyName));
        }

        public override void Draw (CoreGraphics.CGRect rect)
        {
            base.Draw (rect);

            this.BackgroundColor = UIColor.Green;

            BoxControlProperty = _control.CheckedValue;

            Action OnImageViewTapped = () => {
                if(BoxControlProperty)
                    imageView.Image = UIImage.FromFile("test1.png");
                else
                    imageView.Image = UIImage.FromFile("test2.png");
            };

            var tapGesture = new UIGestureRecognizer ();
            tapGesture.AddTarget (OnImageViewTapped);
            imageView.AddGestureRecognizer (tapGesture);
        }
    }

BoxControl:

public class BoxControl :View
    {

        public static readonly BindableProperty BoxControlProperty = BindableProperty.Create<BoxControl, bool>(z=>z.CheckedValue,false);

        public bool CheckedValue
        {
            get{
                return (bool)GetValue (BoxControlProperty);
            }
            set{ 
                SetValue (BoxControlProperty, value);
            }
        }

    }

In the above code the onDraw method is not getting called. While debugging the Xamarin Studio crashes if we keep any breakpoint in

protected override void OnElementChanged (ElementChangedEventArgs<CheckBoxControl> e) method or Draw method.
1

1 Answers

1
votes

Do you have the DependencyService export line at the top of your renderer file?

Similar to:

[assembly: ExportRenderer (typeof (BoxControl ), typeof (BoxControlRenderer))]

I don't see it there, so I have to ask. Then again, I haven't used the most recent version of Xamarin.Forms (1.3) yet. This is how I did custom renderers for a sample project: http://www.joesauve.com/using-xamarin-auth-with-xamarin-forms/. Look for the iOS section about 1/3 of the way down the page.