Xamarin.android 表单Android自定义内容页面呈现器不呈现视图

Xamarin.android 表单Android自定义内容页面呈现器不呈现视图,xamarin.android,xamarin.forms,Xamarin.android,Xamarin.forms,解释了如何为Android、iOS等创建自定义页面渲染器。我根据文档进行了尝试,但不知道为什么它不适用于Android。不过,它确实适用于iOS 共享内容页类: public class SecondPage : ContentPage { public SecondPage() { Content = new StackLayout { Children = {

解释了如何为Android、iOS等创建自定义页面渲染器。我根据文档进行了尝试,但不知道为什么它不适用于Android。不过,它确实适用于iOS

共享内容页类:

public class SecondPage : ContentPage
    {
        public SecondPage()
        {
            Content = new StackLayout
            {
                Children = {
                    new Label { Text = "Hello ContentPage" }
                }
            };
        }
    }
Android的自定义页面渲染器类:

[assembly: ExportRenderer(typeof(SecondPage), typeof(SecondPageRenderer))]
namespace RenderFormsTest.Droid
{
    public class SecondPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                return;
            }

            InitView();
        }

        void InitView()
        {
            var context = Forms.Context;
            string[] os = { "Android", "iOS", "Windows" };
            var ll = new LinearLayout(context);
            ll.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
            ll.SetBackgroundColor(Android.Graphics.Color.IndianRed);

            var rg = new RadioGroup(context);
            for (int index = 0; index < os.Length; index++)
            {
                rg.AddView(new RadioButton(context) { Text = os[index] });
            }

            ll.AddView(rg);

            AddView(ll);
        }
    }
}
[程序集:ExportRenderer(typeof(SecondPage)、typeof(SecondPageRenderer))]
命名空间RenderFormsTest.Droid
{
公共类SecondPageRenderer:PageRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | Element==null)
{
返回;
}
InitView();
}
void InitView()
{
var-context=Forms.context;
字符串[]os={“Android”、“iOS”、“Windows”};
var ll=新的线性布局(上下文);
ll.LayoutParameters=新的LayoutParams(LayoutParams.MatchParent,LayoutParams.MatchParent);
ll.SetBackgroundColor(Android.Graphics.Color.IndianRed);
var rg=新的放射组(上下文);
for(int index=0;index

你能告诉我出了什么问题吗?

我也有类似的问题。看来布局不正确。尝试在PageRenderer.OnLayout中进行设置:

[assembly: ExportRenderer(typeof(SecondPage), typeof(SecondPageRenderer))]
namespace RenderFormsTest.Droid
{
    protected Android.Views.View NativeView;

    public class SecondPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                if (Element == null)
                    NativeView = null;

                return;
            }

            InitView();
        }

        void InitView()
        {
            var context = Forms.Context;
            string[] os = { "Android", "iOS", "Windows" };
            var ll = new LinearLayout(context);
            ll.LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
            ll.SetBackgroundColor(Android.Graphics.Color.IndianRed);

            var rg = new RadioGroup(context);
            for (int index = 0; index < os.Length; index++)
            {
                rg.AddView(new RadioButton(context) { Text = os[index] });    
            }

            ll.AddView(rg);

            AddView(ll);

            NativeView = ll;
        }

        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            base.OnLayout(changed, l, t, r, b);

            if ( NativeView != null )
            { 
                var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly);
                var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly);

                NativeView.Measure(msw, msh);
                NativeView.Layout(0, 0, r - l, b - t);
            }
        }
    }
}
[程序集:ExportRenderer(typeof(SecondPage)、typeof(SecondPageRenderer))]
命名空间RenderFormsTest.Droid
{
受保护的Android.Views.View NativeView;
公共类SecondPageRenderer:PageRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement!=null | | Element==null)
{
if(元素==null)
NativeView=null;
返回;
}
InitView();
}
void InitView()
{
var-context=Forms.context;
字符串[]os={“Android”、“iOS”、“Windows”};
var ll=新的线性布局(上下文);
ll.LayoutParameters=新的LayoutParams(LayoutParams.MatchParent,LayoutParams.MatchParent);
ll.SetBackgroundColor(Android.Graphics.Color.IndianRed);
var rg=新的放射组(上下文);
for(int index=0;index
您能先告诉我们您预期会发生什么以及实际发生什么吗?@GeraldVersluis预期的结果是,当我导航到SeconPage时,应该显示(呈现)一个包含三个单选按钮的RadioGroup视图。但目前我只能看到从ContentPage呈现的标签,即“Hello ContentPage”,在Android中看不到任何其他视图(或从特定于平台的自定义SecondPageRenderer类呈现),页面呈现器只是一个视图组,请尝试更改自定义视图的z索引,查看内容happens@Florian,您能否解释为什么需要覆盖OnLayout?Docs没有提到任何关于它的内容(或者可能会在其他配方中解释,我不确定)。顺便说一句,OnLayout按预期完成了工作。@user2618875,我不能肯定。我也希望它的行为是默认的。我在许多样品中发现了类似的处理方法。例如@user2618875“请注意,在Android上,还需要覆盖OnLayout方法来对视图执行测量和布局操作。”