Xamarin表单自定义渲染器ios

时间:2015-03-20 17:22:34

标签: ios xamarin xamarin.forms

我正在尝试创建自定义控件。 以下是我的代码

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

    }

我的BoxControlView视图:

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

    }

在上面的代码中,没有调用onDraw方法。 如果我们在

中保留任何断点,则调试Xamarin Studio会崩溃
protected override void OnElementChanged (ElementChangedEventArgs<CheckBoxControl> e) method or Draw method.

1 个答案:

答案 0 :(得分:1)

您是否在渲染器文件的顶部有DependencyService导出行?

类似于:

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

我没有看到它,所以我不得不问。然后,我还没有使用最新版本的Xamarin.Forms(1.3)。这就是我为示例项目执行自定义渲染器的方式:http://www.joesauve.com/using-xamarin-auth-with-xamarin-forms/。在页面下方1/3左右查找iOS部分。