您好我正在尝试在Android中为boxview制作渲染器。我需要添加渲染新视图。视图无法显示。有人可以告诉我什么是错的并解释我是否有效?
这是我的渲染器中的方法。
protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e) {
base.OnElementChanged(e);
var activity = this.Context as Activity;
if (e.OldElement == null) {
// Connect the control to its renderer.
PKInputView inputView = (PKInputView)this.Element;
var viewGroup = (global::Android.Views.ViewGroup)ViewGroup;
//Creating Layout
global::Android.Widget.RelativeLayout layout = new global::Android.Widget.RelativeLayout(viewGroup.Context);
//Creating TextView
global::Android.Widget.TextView text = new global::Android.Widget.TextView(this.Context);
text.Text = "ahoj";
text.SetHeight(30);
text.SetTextColor(global::Android.Graphics.Color.Red);
//Set up layers
layout.AddView(text);
viewGroup.AddView(layout);
}
答案 0 :(得分:1)
将e.OldElement替换为this.Control:
if (this.Control == null) {
删除它,因为它会引发异常(除非BoxView继承自PKInputView):
PKInputView inputView = (PKInputView)this.Element;
然后在if语句范围的末尾设置本机视图:
this.SetNativeControl(viewGroup);
}
我认为渲染器看起来像这样:
public class BoxViewRenderer : ViewRenderer<BoxView, global::Android.Views.ViewGroup>
答案 1 :(得分:0)
我必须包括全班,因为你的建议不起作用。
[assembly: ExportRenderer(typeof(PKInputView), typeof(PKInputViewRenderer))]
namespace takeapayment.Droid.Renderers {
public class PKInputViewRenderer : BoxRenderer, CCDataSource {
/*
//header
private global::Android.Widget.AbsoluteLayout header = Android.Widget.AbsoluteLayout();
private TextView title = new TextView();
private global::Android.Widget.Button saveButton = new Button();
*/
protected void OnCreate(Bundle savedInstanceState) {
this.OnCreate(savedInstanceState);
}
protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e) {
base.OnElementChanged(e);
var activity = this.Context as Activity;
if (e.OldElement == null) {
// Connect the control to its renderer.
PKInputView inputView = (PKInputView)this.Element;
var viewGroup = (global::Android.Views.ViewGroup)ViewGroup;
//Creating Layout
global::Android.Widget.RelativeLayout layout = new global::Android.Widget.RelativeLayout(viewGroup.Context);
//Creating TextView
global::Android.Widget.TextView text = new global::Android.Widget.TextView(this.Context);
text.Text = "ahoj";
text.SetHeight(30);
text.SetTextColor(global::Android.Graphics.Color.Red);
//Set up layers
layout.AddView(text);
viewGroup.AddView(layout);
}
}
}
答案 2 :(得分:0)
如果要替换控件,则应从viewrenderer派生渲染器。 试试这些代码。而这个vdo
[assembly: ExportRenderer(typeof(PKInputView), typeof(PKInputViewRenderer))]
public class PKInputViewRenderer : ViewRenderer<PKInputView,global::Android.Widget.TextView>, CCDataSource
{
protected override void OnElementChanged(ElementChangedEventArgs<PKInputView> e)
{
base.OnElementChanged(e);
// Connect the control to its renderer.
PKInputView inputView = e.NewElement ?? e.OldElement;;
// Creating TextView
global::Android.Widget.TextView text = new global::Android.Widget.TextView(this.Context);
text.Text = "ahoj";
text.SetHeight(30);
text.SetTextColor(global::Android.Graphics.Color.Red);
SetNativeControl(text);
}
}
答案 3 :(得分:0)