Hi all,
I'm trying to programmatically add views to a vertical StackLayout that is wrapped in a Scrollview and reposition a subview based on the position of another view on my page.
Here's part of my code:
public void RepositionImage(){
this.Children.Add(Container1);
this.Children.Add(Container2);
// MyImage should be horizontally aligned to referenced
ViewContainer2.Children.Add(MyImage, new Point(referencedView.Bounds.X, 0));
}
I'm trying to figure out when to call the above code so that (1) the 2 containers are added to the StackLayout and (2) are positioned correctly and (3) MyImage is actually positioned at the same X-position as referencedView.
I've tried several ways to achieve this, for instance:
1) Call RepositionImage in OnSizeAllocated of the StackLayout:
- On both iOS and Android the containers are added, but they are not displayed correctly: the StackLayout's height is not updated.
The strange thing is that on Android, if I trigger the keyboard, all elements automatically jump to the correct positions. On iOS that does not happen.
2) Call RepositionImage in OnAppearing of the ContentPage:
- On iOS everything is displayed perfectly now
- On Android referencedView does not have Bounds at that moment, so MyImage is not positioned correctly.
This seems strange to me since I'd think OnAppearing is called right before the page is displayed (and I guess everything should have a position then).
I have 2 questions:
1) Specifically for my above question: where do I put my RepositionImage call so that it works perfectly on iOS and Android?
2) Is there any documentation on the Page and View lifecycles in Xamarin Forms.
When are OnAppearing, OnSizeAllocate, LayoutChildren,... called?
I'd like to know for both iOS and Android, since they seem to behave differently.
Thanks for the help,
Thomas