c# - What is the Xamarin.Forms equivalent of layoutSubviews? -


i'm porting large ios codebase xamarin.forms app. have lot of custom views perform layout logic making calculations in -layoutsubviews. codebase large me port in time if need reinterpret these calculations in terms of stack or grid layouts. want direct equivalent, can add equivalent subviews our views without worrying go, , method called when view's bounds change inside can set new bounds of subviews. can directly port our existing ios code.

is there equivalent in xamarin.forms -layoutsubviews?

you can create own layout deriving xamarin.forms.layout class.

public class customlayout : layout<view> {     public customlayout ()     {      } } 

the layout must override layoutchildren method. method responsible positioning children on screen.

children can measured using getsizerequest method, return both desired size , minimum size child desires.

protected override void layoutchildren (double x, double y, double width, double height) {     (int = 0; < children.count; i++) {         var child = (view) children[i];         // skip invisible children          if(!child.isvisible)              continue;         var childsizerequest = child.getsizerequest (double.positiveinfinity, height);         var childwidth = childsizerequest.request.width;         layoutchildintoboundingregion (child, new rectangle (x, y, childwidth, height));         x += childwidth;     } } 

this method automatically called whenever layout needs recomputed. if layout consists of hardcoded or fixed size elements, hard code sizes algorithm instead of measuring. getsizerequest calls of expensive calls can made, , not predictable in runtime subtree may arbitrary complex. fixing size great way performance boost if dynamic sizing not required.

implementing onsizerequest required make sure new layout sized correctly when placed inside other layouts. during layout cycles method may called many times depending on layout above , how many layout exceptions required resolve current layout hierarchy.

protected override sizerequest onsizerequest (double widthconstraint, double heightconstraint) {     var height = 0;     var minheight = 0;     var width = 0;     var minwidth = 0;      (int = 0; < children.count; i++) {         var child = (view) children[i];         // skip invisible children          if(!child.isvisible)              continue;         var childsizerequest = child.getsizerequest (double.positiveinfinity, height);         height = math.max (height, childsizerequest.minimum.height);         minheight = math.max (minheight, childsizerequest.minimum.height);         width += childsizerequest.request.width;         minwidth += childsizerequest.minimum.width;     }      return new sizerequest (new size (width, height), new size (minwidth, minheight)); } 

you can read whole tutorial of how create custom layout here.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -