Startup Performance - davidortinau/ControlGallery GitHub Wiki
💬 Copilot Chat Prompt
Review my .NET MAUI startup code, especially MauiProgram.CreateMauiApp and App.xaml.cs. Identify synchronous operations, service initialization, or large dictionary loading that could slow startup. Suggest what can be deferred or lazily loaded.
Start-up time impacts the first impression of your app. Slower startup is often caused by unnecessary synchronous work.
- Heavy logic in
MauiProgram.CreateMauiApp()
- DI container scanning assemblies or loading all services
- Eager loading of large resource dictionaries or services
- Use deferred service loading in your dependency injection setup:
builder.Services.AddSingleton<IMyService, MyService>(); // Eager load builder.Services.AddSingleton(provider => new Lazy<IMyService>(() => new MyService())); // Lazy load
- Move background or optional tasks to after app load:
MainPage.Appearing += async (_, _) => await PrefetchDataAsync();
- Avoid synchronous file IO or large configuration parsing in
CreateMauiApp()
:var config = File.ReadAllText("config.json"); // ❌ Synchronous call var config = await File.ReadAllTextAsync("config.json"); // ✅ Async alternative