{"record":{"id":"283c573535eaab14","repo":"dotnet/maui","slug":"mauicontext-not-set","errorCode":null,"errorMessage":"MauiContext not set","messagePattern":"MauiContext not set","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Compatibility/Handlers/VisualElementRenderer.cs","lineNumber":59,"sourceCode":"\t\t\t[AutomationProperties.HelpTextProperty.PropertyName] = MapAutomationPropertiesHelpText,\n\t\t\t[AutomationProperties.LabeledByProperty.PropertyName] = MapAutomationPropertiesLabeledBy,\n#pragma warning restore CS0618 // Type or member is obsolete\n#endif\n\t\t};\n\n\t\tpublic static CommandMapper<TElement, IPlatformViewHandler> VisualElementRendererCommandMapper = new CommandMapper<TElement, IPlatformViewHandler>(ViewHandler.ViewCommandMapper);\n\n#if IOS || MACCATALYST\n\t\tWeakReference<TElement>? _virtualView;\n\t\tTElement? _tempElement;\n#else\n\t\tTElement? _virtualView;\n#endif\n\t\tIMauiContext? _mauiContext;\n\t\tinternal IPropertyMapper _mapper;\n\t\tinternal readonly CommandMapper? _commandMapper;\n\t\tinternal readonly IPropertyMapper _defaultMapper;\n\t\tprotected IMauiContext MauiContext => _mauiContext ?? throw new InvalidOperationException(\"MauiContext not set\");\n#if IOS || MACCATALYST\n\t\tpublic TElement? Element => _tempElement ?? (_virtualView is not null && _virtualView.TryGetTarget(out var target) ? target : null);\n#else\n\t\tpublic TElement? Element => _virtualView;\n#endif\n\t\tprotected bool AutoPackage { get; set; } = true;\n\n#if ANDROID\n\t\tpublic VisualElementRenderer(Context context) : this(context, VisualElementRendererMapper, VisualElementRendererCommandMapper)\n\t\t{\n\t\t}\n#else\n\t\tpublic VisualElementRenderer() : this(VisualElementRendererMapper, VisualElementRendererCommandMapper)\n\t\t{\n\t\t}\n#endif\n\n","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Compatibility/Handlers/VisualElementRenderer.cs#L41-L77","documentation":"Thrown by the VisualElementRenderer base class when the MauiContext property is accessed but _mauiContext has not been set. MauiContext is the bridge between the MAUI abstraction layer and the platform-specific context (DI services, handlers, etc.). Every renderer needs it to resolve handlers, converters, and other services. The property getter throws InvalidOperationException if null, indicating the renderer was used before the framework assigned its context.","triggerScenarios":"At line 59: `protected IMauiContext MauiContext => _mauiContext ?? throw new InvalidOperationException(\"MauiContext not set\")`. Accessing MauiContext before the framework has called SetMauiContext (or equivalent). This typically happens when: (1) a renderer method is called during construction before context assignment; (2) a custom renderer accesses MauiContext in an overridden method that fires too early in the lifecycle; (3) the handler's platform view was created outside the normal MAUI handler pipeline; (4) DI/initialization ordering failed in MauiProgram.","commonSituations":"1) Custom renderer accessing MauiContext in its constructor or an early lifecycle method (e.g., OnElementChanged before base sets context). 2) Creating platform views manually instead of through the handler infrastructure. 3) MAUI initialization (MauiProgram.CreateMauiApp) incomplete or failed, so context was never propagated. 4) Using the compatibility VisualElementRenderer outside the standard handler registration. 5) Lifecycle ordering issues in specific MAUI versions.","solutions":["Avoid accessing MauiContext in the renderer constructor — defer to OnElementChanged or later lifecycle methods.","Ensure MauiProgram is fully configured (UseMauiApp, builder.Build()) before any rendering occurs.","If using a custom renderer, ensure it is registered through the standard handler/registration pipeline so the framework sets MauiContext.","Verify that the app's startup sequence creates the MAUI application before any platform view tries to use it.","Check MAUI version compatibility — update to a stable release if using pre-release builds with lifecycle bugs."],"exampleFix":"// before (custom renderer)\npublic class MyRenderer : VisualElementRenderer<MyView>\n{\n    public MyRenderer(Context context) : base(context)\n    {\n        var service = MauiContext.Services; // throws — context not set yet\n    }\n}\n\n// after\npublic class MyRenderer : VisualElementRenderer<MyView>\n{\n    public MyRenderer(Context context) : base(context) { }\n\n    protected override void OnElementChanged(ElementChangedEventArgs<MyView> e)\n    {\n        base.OnElementChanged(e);\n        if (e.NewElement != null)\n        {\n            var service = MauiContext.Services; // safe — context is set by now\n        }\n    }\n}","handlingStrategy":"validation","validationCode":"// In custom renderers, check before accessing MauiContext\nif (_mauiContext == null)\n    return; // defer until context is set\nvar service = MauiContext.Services;","typeGuard":"public bool HasMauiContext => _mauiContext != null;","tryCatchPattern":null,"preventionTips":["Never access MauiContext in the renderer constructor — use OnElementChanged.","Ensure MauiProgram is fully initialized before any rendering occurs.","Register custom renderers through the standard handler pipeline."],"tags":["maui","renderer","mauicontext","lifecycle","initialization"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}