【发布时间】:2015-03-20 17:22:33
【问题描述】:
我正在尝试创建自定义控件。 下面是我的代码
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.
【问题讨论】:
标签: ios xamarin xamarin.forms