微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

升级到 Xamarin.Forms 5.0 后,不会在自定义控件中调用 LayoutChildren 方法 重现步骤预期行为

如何解决升级到 Xamarin.Forms 5.0 后,不会在自定义控件中调用 LayoutChildren 方法 重现步骤预期行为

我创建了一个自定义控件,它是从 Xamarin.Forms 中的 ContentView 扩展而来的。我在表单中使用 LayoutChildren 覆盖方法为某些场景布局了 Content。这在 XF 4.8 版之前都可以正常工作。升级到 Xamarin.Forms 5.0 后,不会调用LayouChildren 覆盖方法,UWP 中也不会显示内容。我怀疑,在 Xamarin.Forms 5.0 中,未对内容执行布局/排列操作。

注意:如果我在网格中添加相同的自定义控件,则会显示自定义控件的内容

  1. 我只是创建了一个小的自定义控件来重现我的问题。在 Xamarin.Forms 中创建自定义控件

    public class CustomContentView: ContentView
     {
         public CustomContentView()
         {
         }
    
         protected override SizeRequest OnMeasure(double widthConstraint,double heightConstraint)
         {
             return base.OnMeasure(widthConstraint,heightConstraint);
         }
         protected override void LayoutChildren(double x,double y,double width,double height)
         {
             base.LayoutChildren(x,y,width,height);
         }
     }
    
  2. 在 UWP 渲染器项目中为此 Forms 控件创建了自定义渲染器。原生 UWP 控件是从 ContentPresenter 扩展而来的。我已将 Forms 内容转换为本机并设置为内容控件的内容。然后将内容控制分配给本机控制。

UWP 中的原生控件

public class CustomView : ContentPresenter
    {
        public CustomView()
        {
        }
    }

渲染器类

public class Customrenderer : VisualElementRenderer<CustomControl.CustomContentView,CustomView>
    {
        private CustomView customView;
        public Customrenderer()
        {
            this.Autopackage = false;
        }
        protected override void OnElementChanged(ElementChangedEventArgs<CustomContentView> e)
        {
            if (e.NewElement == null)
            {
                return;
            }

            this.customView = new CustomView();
            this.customView.DataContext = this.Element.BindingContext;
            this.SetNativeControl(this.customView);

            if (Element.Content != null)
            {
                var renderer = Element.Content.GetorCreateRenderer();
                var contentControl = new ContentControl();

                contentControl.Content = renderer.ContainerElement;
                this.customView.Content = contentControl;
            }
            base.OnElementChanged(e);
        }
    }

重现步骤

  1. 运行附加的示例(复制链接中给出的示例链接
  2. 在我的自定义控件中添加了按钮控件作为内容。但它不显示

预期行为

应该显示内容(按钮)

Sample link with custom control:

有人请确认这是自定义控件(XF5.0)还是框架方面的问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。