unoplatform/uno · error · InvalidOperationException

Unable to find [MapGrid] template part

Error message

Unable to find [MapGrid] template part

What it means

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.

Source

Thrown at src/AddIns/Uno.UI.Maps/MapPresenter.Android.cs:64

			_internalMapView.GetMapAsync(_callback = new MapReadyCallback(OnMapReady));

			_internalMapView.OnCreate(null); // This otherwise the map does not appear

			Loaded += (s, e) => OnControlLoaded();
			Unloaded += (s, e) => OnControlUnloaded();
		}

		protected override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			UpdateOwnerSubscriptions();

			_mapGrid = GetTemplateChild("MapGrid") as Grid;
			_layerGrid = GetTemplateChild("LayerGrid") as Grid;

			if (_mapGrid == null)
			{
				throw new InvalidOperationException("Unable to find [MapGrid] template part");
			}

			if (_layerGrid == null)
			{
				throw new InvalidOperationException("Unable to find [LayerGrid] template part");
			}

			_mapGrid.Children.Add(_internalMapView);
		}

		private IDisposable UpdateOwnerSubscriptions(MapControl owner)
		{
			OnCenterChanged();
			var disposables = new CompositeDisposable();
			owner.RegisterDisposablePropertyChangedCallback(MapControl.CenterProperty, (s, e) => OnCenterChanged()).DisposeWith(disposables);
			owner.RegisterDisposablePropertyChangedCallback(MapControl.ZoomLevelProperty, (s, e) => OnCenterChanged()).DisposeWith(disposables);
			return disposables;
		}

View on GitHub (pinned to 0418340488)

Solutions

  1. 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).
  2. Do not override the default template unless necessary; if you must, copy the default template and only restyle non-template-part elements.
  3. Run XAML validation / visual-tree inspection at runtime to confirm GetTemplateChild finds the named parts.
  4. Check that the custom Style's TargetType matches and the template is actually applied (vs. falling back to base).

Example fix

// before — custom template missing MapGrid
<Style TargetType="maps:MapControl">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate><Grid/></ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

// after — include required template parts
<ControlTemplate>
  <Grid>
    <Grid x:Name="MapGrid"/>
    <Grid x:Name="LayerGrid"/>
  </Grid>
</ControlTemplate>
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the template contains the named part before applying
var mapGrid = mapControl.GetTemplateChild("MapGrid") as Grid;
if (mapGrid == null) throw new InvalidOperationException("Custom template missing MapGrid");

Type guard

bool TemplateHasMapGrid(Control c)
    => c.GetTemplateChild("MapGrid") is Grid;

Try / catch

try { /* OnApplyTemplate logic */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("MapGrid"))
{ Log.Error("MapControl template is missing the MapGrid part"); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/bed3ad483d955ac3. Report an issue: GitHub.