Xamarin.Forms Android自定义ContentPage PageRenderer不呈现视图

时间:2016-09-22 05:24:26

标签: xamarin.android xamarin.forms

This doc解释了如何为Android,iOS等创建自定义PageRenderer。我按照文档尝试过,但不知道为什么它不能用于Android。但它适用于iOS。

共享ContentPage类:

public class SecondPage : ContentPage
    {
        public SecondPage()
        {
            Content = new StackLayout
            {
                Children = {
                    new Label { Text = "Hello ContentPage" }
                }
            };
        }
    }

Android的自定义PageRenderer类:

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

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

1 个答案:

答案 0 :(得分:2)

我有类似的问题。看起来布局不正确。尝试在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);
            }
        }
    }
}