{"record":{"id":"bed3ad483d955ac3","repo":"unoplatform/uno","slug":"unable-to-find-mapgrid-template-part","errorCode":null,"errorMessage":"Unable to find [MapGrid] template part","messagePattern":"Unable to find \\[MapGrid\\] template part","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AddIns/Uno.UI.Maps/MapPresenter.Android.cs","lineNumber":64,"sourceCode":"\t\t\t_internalMapView.GetMapAsync(_callback = new MapReadyCallback(OnMapReady));\n\n\t\t\t_internalMapView.OnCreate(null); // This otherwise the map does not appear\n\n\t\t\tLoaded += (s, e) => OnControlLoaded();\n\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}","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/unoplatform/uno/blob/04183404888ea21b4e5bfa3493e8d5f15e972c3a/src/AddIns/Uno.UI.Maps/MapPresenter.Android.cs#L46-L82","documentation":"InvalidOperationException thrown by MapPresenter.OnApplyTemplate (Android) when GetTemplateChild(\"MapGrid\") does not return a Grid — i.e. the control's ControlTemplate is missing the required 'MapGrid' template part. MapPresenter declares two required template parts (MapGrid, LayerGrid) defined in Generic.xaml; overriding the template without them breaks the control.","triggerScenarios":"A developer sets a custom ControlTemplate/Style on MapControl (or MapPresenter) that omits a Grid named 'MapGrid', or names it differently. OnApplyTemplate then fails to retrieve it and throws before the native GoogleMap view can be parented.","commonSituations":"Restyling MapControl with a custom Style that redefines the ControlTemplate but forgets the x:Name=\"MapGrid\" and x:Name=\"LayerGrid\" Grid elements; merging a theme that overrides Generic.xaml; a malformed template from a design tool.","solutions":["Ensure any custom ControlTemplate for MapControl/MapPresenter includes <Grid x:Name=\"MapGrid\"/> and <Grid x:Name=\"LayerGrid\"/> (see src/AddIns/Uno.UI.Maps/themes/Generic.xaml).","Do not override the default template unless necessary; if you must, copy the default template and only restyle non-template-part elements.","Run XAML validation / visual-tree inspection at runtime to confirm GetTemplateChild finds the named parts.","Check that the custom Style's TargetType matches and the template is actually applied (vs. falling back to base)."],"exampleFix":"// before — custom template missing MapGrid\n<Style TargetType=\"maps:MapControl\">\n  <Setter Property=\"Template\">\n    <Setter.Value>\n      <ControlTemplate><Grid/></ControlTemplate>\n    </Setter.Value>\n  </Setter>\n</Style>\n\n// after — include required template parts\n<ControlTemplate>\n  <Grid>\n    <Grid x:Name=\"MapGrid\"/>\n    <Grid x:Name=\"LayerGrid\"/>\n  </Grid>\n</ControlTemplate>","handlingStrategy":"validation","validationCode":"// Confirm the template contains the named part before applying\nvar mapGrid = mapControl.GetTemplateChild(\"MapGrid\") as Grid;\nif (mapGrid == null) throw new InvalidOperationException(\"Custom template missing MapGrid\");","typeGuard":"bool TemplateHasMapGrid(Control c)\n    => c.GetTemplateChild(\"MapGrid\") is Grid;","tryCatchPattern":"try { /* OnApplyTemplate logic */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"MapGrid\"))\n{ Log.Error(\"MapControl template is missing the MapGrid part\"); }","preventionTips":["Always include x:Name=\"MapGrid\" Grid in custom MapControl templates.","Copy the default Generic.xaml template as the starting point.","Run visual-tree validation in tests to catch missing template parts."],"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"}