{"record":{"id":"20731656f1cf4f62","repo":"unoplatform/uno","slug":"unable-to-find-layergrid-template-part","errorCode":null,"errorMessage":"Unable to find [LayerGrid] template part","messagePattern":"Unable to find \\[LayerGrid\\] template part","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AddIns/Uno.UI.Maps/MapPresenter.Android.cs","lineNumber":69,"sourceCode":"\t\t\tUnloaded += (s, e) => OnControlUnloaded();\n\t\t}\n\n\t\tprotected override void OnApplyTemplate()\n\t\t{\n\t\t\tbase.OnApplyTemplate();\n\t\t\tUpdateOwnerSubscriptions();\n\n\t\t\t_mapGrid = GetTemplateChild(\"MapGrid\") as Grid;\n\t\t\t_layerGrid = GetTemplateChild(\"LayerGrid\") as Grid;\n\n\t\t\tif (_mapGrid == null)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\"Unable to find [MapGrid] template part\");\n\t\t\t}\n\n\t\t\tif (_layerGrid == null)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException(\"Unable to find [LayerGrid] template part\");\n\t\t\t}\n\n\t\t\t_mapGrid.Children.Add(_internalMapView);\n\t\t}\n\n\t\tprivate IDisposable UpdateOwnerSubscriptions(MapControl owner)\n\t\t{\n\t\t\tOnCenterChanged();\n\t\t\tvar disposables = new CompositeDisposable();\n\t\t\towner.RegisterDisposablePropertyChangedCallback(MapControl.CenterProperty, (s, e) => OnCenterChanged()).DisposeWith(disposables);\n\t\t\towner.RegisterDisposablePropertyChangedCallback(MapControl.ZoomLevelProperty, (s, e) => OnCenterChanged()).DisposeWith(disposables);\n\t\t\treturn disposables;\n\t\t}\n\n\t\tprivate void OnMapReady(GoogleMap map)\n\t\t{\n\t\t\t_map = map;\n\t\t\t_map.MyLocationEnabled = false;","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.UI.Maps/MapPresenter.Android.cs#L51-L87","documentation":"InvalidOperationException thrown by MapPresenter.OnApplyTemplate (Android) when GetTemplateChild(\"LayerGrid\") does not return a Grid. LayerGrid is the second required template part (used for overlay layers); its absence prevents the control from initializing its layering surface.","triggerScenarios":"A custom ControlTemplate for MapControl omits the 'LayerGrid' named Grid, or it is present but not of type Grid (e.g. a Canvas), so the 'as Grid' cast yields null.","commonSituations":"Restyling MapControl and replacing LayerGrid with a different panel type; copy-paste template that renames the element; merging an incomplete theme resource dictionary.","solutions":["Ensure the template contains <Grid x:Name=\"LayerGrid\"/> of type Grid exactly.","Copy the default template from Generic.xaml and only restyle visuals, preserving both named Grid parts.","Verify the element type — using a Canvas or Border named 'LayerGrid' will fail the 'as Grid' cast.","Remove the custom Style override to confirm the default template works, then reintroduce changes incrementally."],"exampleFix":"// before — LayerGrid is a Canvas (wrong type)\n<Canvas x:Name=\"LayerGrid\"/>\n\n// after — correct type\n<Grid x:Name=\"LayerGrid\"/>","handlingStrategy":"validation","validationCode":"var layerGrid = mapControl.GetTemplateChild(\"LayerGrid\") as Grid;\nif (layerGrid == null) throw new InvalidOperationException(\"Custom template missing LayerGrid\");","typeGuard":"bool TemplateHasLayerGrid(Control c)\n    => c.GetTemplateChild(\"LayerGrid\") is Grid;","tryCatchPattern":"try { /* OnApplyTemplate logic */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"LayerGrid\"))\n{ Log.Error(\"MapControl template is missing the LayerGrid part\"); }","preventionTips":["Always include x:Name=\"LayerGrid\" of type Grid in custom templates.","Do not substitute a different panel type for LayerGrid.","Validate the template against the default before shipping."],"tags":["maps","control-template","template-part","android","uno-platform"],"backgroundTag":null,"analyzedSha":"04183404888ea21b4e5bfa3493e8d5f15e972c3a","analyzedAt":"2026-08-13T21:08:11.651Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}