【问题标题】:Xamarin.Forms Android custom ContentPage PageRenderer not rendering viewsXamarin.Forms Android 自定义 ContentPage PageRenderer 不呈现视图
【发布时间】:2016-09-22 05:24:26
【问题描述】:

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

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

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

【问题讨论】:

  • 您能先告诉我们您预期会发生什么以及实际会发生什么吗?
  • @GeraldVersluis 预期结果是,当我导航到 SeconPage 时,应显示(渲染)具有三个单选按钮的 RadioGroup 视图。但目前我只能看到从 ContentPage 呈现的标签,即“Hello ContentPage”,并且没有任何其他视图可见(或从特定于平台的自定义 SecondPageRenderer 类呈现)
  • 在 Android 中,页面渲染器只是一个视图组,尝试更改自定义视图的 z-index,看看会发生什么

标签: xamarin.android xamarin.forms


【解决方案1】:

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

【讨论】:

  • @Florian,你能解释一下为什么需要覆盖 OnLayout 吗? Docs 没有提到任何关于它的内容(或者可能在我不确定的其他食谱中解释)。 BTW OnLayout 按预期完成工作。
  • @user2618875,我不能肯定。我也希望它具有默认行为。我在许多样本中发现了类似的处理方式。例如。 Blogpost
  • @user2618875 docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/… "请注意,在 Android 上,还需要重写 OnLayout 方法才能对视图执行测量和布局操作。"
猜你喜欢
  • 2017-08-29
  • 1970-01-01
  • 2020-04-29
  • 2014-07-24
  • 2019-04-03
  • 2020-09-02
  • 2016-01-22
  • 2015-12-12
  • 2013-05-11
相关资源
最近更新 更多