gpt4 book ai didi

ios - Xamarin Forms 自定义渲染器 ios

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:16:51 24 4
gpt4 key购买 nike

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

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

盒子控件:

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.

最佳答案

您的渲染器文件顶部是否有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com