2

I’ve been using Xamarin.Forms for a bit lately and can generally get enough information following the Microsoft docs. But I seem to be stuck now that I need to create a custom renderer, so if someone could help, I’d really appreciate.

I read through the docs on creating a custom render and maybe I missed the part I’m looking for, but I’m not able to tell if as part of OnElementChanged, I also need to render all the children of the Element?

The ContentPage could have a StackLayout for example with several child elements. If I dont have to render these, how do they get rendered/laid-out?

1 Answer 1

0

No, you don't need to render ContentPage's children.

When you create a Custom Renderer you are only extending Xamarin's renderer, meaning you only add functionality (unless you explicitly change something, like redefining a property).

So, basically, the ContentPage render/lay-out the children the same way it renders/lays-out without the Custom Renderer.


Answering your second question, actually the layout and positioning is done without renderers, as said here Xamarin.Forms Layouts.

If you want to dig deeper, you can try to understand how the ContentView (it's the easier one) is positioned by reading the source code. Here the TemplatedView (ContentView's base class) is calling LayoutChildIntoBoundingRegion for each of its children: TemplatedView.

Then this line on the LayoutChildIntoBoundingRegion calls the Layout method of the view: Layout.

The VisualElement.Layout method only gets the rectangle and sets the Bounds of the view (Layout Method). The Bounds setter is called: Setter.

Well, now you can go on and explore more of the source code if you want, but I think you got the idea ;)

Hope it helps!

5
  • Thanks! So I’m guessing each child gets rendered independently of the parent with its own dedicated renderer? If that’s the case, what controls which element gets rendered when? This is so that I can have a clear answer to my second question. Sorry if it’s going into the deep dark depths of Xamarin implementation.
    – AjLearning
    May 12, 2018 at 0:02
  • Yep, it child gets renderer independently, but I didn't quite get the meaning of your question, sorry. Could you elaborate your question a bit? May 12, 2018 at 1:42
  • Thanks. That answers most of my questions. The only bit that I need clarity on is layout. What decides where children get positioned? Is there a layout renderer that runs alongside the view renderer?
    – AjLearning
    May 12, 2018 at 7:41
  • Thank you so much for that. It does give me a deeper understanding. The only missing link in my head now is: the render is responsible for drawing the Element and the TemplatedView decides where each child gets positioned, but who decides that the drawing of the child must happen within the bounds specified? And is the bounding region interpreted as an absolute position on screen? Also, how do you know so much about the inner workings of Xamarin.Forms? :-D
    – AjLearning
    May 13, 2018 at 1:22
  • Sorry for the delay. I don't think something decides that the drawing should be within the bounds, it simply draws the native view using the bounds specified by the Layout (I don't know much about this so I advise you to search the source code if you really want to know). For the second question, yes, it's interpreted as an absolute position (at least at a final stage of positioning), and if you check the view.Bounds property, you'll see it (it doesn't use pixels, but a scaled number like dp, pt, etc). And I know about these things because I'm curious :) May 16, 2018 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.