diff --git a/src/Core/src/MauiContext.cs b/src/Core/src/MauiContext.cs index 6d6dfbc1256d..341f0a423205 100644 --- a/src/Core/src/MauiContext.cs +++ b/src/Core/src/MauiContext.cs @@ -7,8 +7,13 @@ namespace Microsoft.Maui public class MauiContext : IMauiContext { readonly WrappedServiceProvider _services; + readonly Lazy _handlers; + +#if ANDROID + readonly Lazy _context; + + public Android.Content.Context? Context => _context.Value; -#if __ANDROID__ public MauiContext(IServiceProvider services, Android.Content.Context context) : this(services) { @@ -19,17 +24,15 @@ public MauiContext(IServiceProvider services, Android.Content.Context context) public MauiContext(IServiceProvider services) { _services = new WrappedServiceProvider(services ?? throw new ArgumentNullException(nameof(services))); + _handlers = new Lazy(() => _services.GetRequiredService()); +#if ANDROID + _context = new Lazy(() => _services.GetService()); +#endif } public IServiceProvider Services => _services; - public IMauiHandlersFactory Handlers => - Services.GetRequiredService(); - -#if __ANDROID__ - public Android.Content.Context? Context => - Services.GetService(); -#endif + public IMauiHandlersFactory Handlers => _handlers.Value; internal void AddSpecific(TService instance) where TService : class