I've got some XAML ContentPages that have ContentView objects on them. So, typically I size these objects based on this.Width and this.Height, values that are set in the ContentPage. But, if there's not something explicitly calling my ContentView objects AFTER the ContentPage has loaded, the Width and Height values are null because the values aren't yet set.
What I'm trying to figure out is, how can I tell my ContentViews to wait until the ContentPage is done loading before it gets the Width and Height values?
Xamarin.Forms provides two lifecycle-type events:
OnAppearing
OnDisappearing
which can be overridden in Page subclasses.
Now the fact, OnAppearing
will be executed before the screen comes, when you want an Loaded event that needs to be executed right after the screen comes, there is a workaround.
private bool _toggleTemp;
public bool ToggleTemp
{
get => _toggleTemp;
set => SetProperty(ref _toggleTemp, value);
}
LoadingVm.ToggleTemp = true;
<Switch IsToggled="{Binding ToggleTemp}" Toggled="Switch_OnToggled" IsVisible="False" />
private async void Switch_OnToggled(object sender, ToggledEventArgs e)
{
/* Your code goes here... */
}
Please let me know if you have any doubts
I've found another workaround and it is working in Xamarin Forms 4.8:
private bool isStarted = false;
protected override void LayoutChildren(double x, double y, double width, double height)
{
base.LayoutChildren(x, y, width, height);
if (!isStarted)
{
isStarted = true;
// do something
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With