Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

c# - LayoutChildren method is not called in custom control after upgrade to Xamarin.Forms 5.0

I have created a custom control which is extended from ContentView in Xamarin.Forms. I have layout the Content for some scenario using LayoutChildren override method in Forms. This is working fine up to XF version 4.8. After upgrade to Xamarin.Forms 5.0, this LayouChildren override method is not called and Content also not displayed in UWP. I suspect that, in Xamarin.Forms 5.0 layout/arrange operation is not performed for Content.

Note: If i add the same custom control within Grid, custom control's Content is displayed.

  1. I simply created a small custom control to reproduce my issue. Created custom control in 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. Created custom renderer for this Forms control in UWP renderer project. Native UWP control is extended from ContentPresenter. I have converted Forms content to native and set to content control's content. Then assigned content control to Native control.

Native control in UWP

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

Renderer class

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

Steps to Reproduce

  1. Run the attached sample (Sample link given in Reproduction Link)
  2. Added Button control as content in my custom control. But it is not displayed.

Expected Behavior

Content (Button) should be displayed

Sample link with custom control:

Anyone please confirm whether this is custom control(XF5.0) or framework side issue?

question from:https://stackoverflow.com/questions/65832963/layoutchildren-method-is-not-called-in-custom-control-after-upgrade-to-xamarin-f

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...