Xamarin: First call to InitializeComponent slow

350 views Asked by At

The problem is that the first call to a page's InitializeComponent is very slow. Thus the page transition animation is laggy for the first time. StackLayout with a few Labels and a Grid can take like 70 ms on my phone. I removed almost everything from the page and tried to find a bottle neck but it seems that even an empty grid adds a significant amount of time. So my full page takes something like 180 ms and when it's loaded for the first time there is almost no animation and it looks like a freezing app. Consecutive animations take like 15~20 ms.

Answering your potential questions: Yes, I have XAML compilation enabled. And yes, I've tried Release build but it doesn't help much.

Updated

I have added a button and a static grid to an items detail of default Xamarin application created in Visual Studio.

This is current content of the page:

<StackLayout Spacing="20" Padding="15">
    <Label Text="Text:" FontSize="Medium" />
    <Label Text="{Binding Text}" FontSize="Small"/>
    <Label Text="Description:" FontSize="Medium" />
    <Label Text="{Binding Description}" FontSize="Small"/>
    <Button Text="{Binding Text}" />

    <Grid RowSpacing="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Text="Coins:" FontAttributes="Bold" />
        <Label Grid.Row="0" Grid.Column="1" Text="111" HorizontalTextAlignment="End" />

        <Label Grid.Row="0" Grid.Column="2" Text="Total coins:" FontAttributes="Bold" />
        <Label Grid.Row="0" Grid.Column="3" Text="100000" HorizontalTextAlignment="End"/>

        <Label Grid.Row="1" Grid.Column="0" Text="Gold:" FontAttributes="Bold" />
        <Label Grid.Row="1" Grid.Column="1" Text="222" HorizontalTextAlignment="End" />

        <Label Grid.Row="1" Grid.Column="2" Text="Table gold:" FontAttributes="Bold" />
        <Label Grid.Row="1" Grid.Column="3" Text="120 " HorizontalTextAlignment="End"/>
    </Grid>
</StackLayout>

And this is how I measure the time:

public partial class ItemDetailPage : ContentPage
{
    public ItemDetailPage()
    {
        var start = DateTime.Now.ToUniversalTime().Millisecond;
        InitializeComponent();
        var end = DateTime.Now.ToUniversalTime().Millisecond;
        Debug.WriteLine($"ItemDetailPage.InitializeComponent: {end - start}");
        BindingContext = new ItemDetailViewModel();
    }
}
0

There are 0 answers