我正在尝试创建自定义控件。
下面是我的代码
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 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);
}
}
}
在上面的代码中,onDraw 方法没有被调用。
如果我们在 中保留任何断点,则在调试 Xamarin Studio 时会崩溃
protected override void OnElementChanged (ElementChangedEventArgs<CheckBoxControl> e) method or Draw method.
Best Answer-推荐答案 strong>
渲染器文件顶部是否有 DependencyService 导出行?
类似于:
[assembly: ExportRenderer (typeof (BoxControl ), typeof (BoxControlRenderer))]
我没有看到它,所以我不得不问。再说一次,我还没有使用最新版本的 Xamarin.Forms (1.3)。这就是我为示例项目定制渲染器的方式:http://www.joesauve.com/using-xamarin-auth-with-xamarin-forms/ .在页面下方约 1/3 处查找 iOS 部分。
关于ios - Xamarin Forms 自定义渲染器 ios,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/29172388/
|