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 (iOS) when GetTemplateChild("MapGrid") does not return a Grid. Identical contract to the Android variant (error 26): the iOS presenter requires a 'MapGrid' Grid template part into which it parents the native MKMapView.

Source

Thrown at src/AddIns/Uno.UI.Maps/MapPresenter.iOS.cs:48

		private MKMapView _internalMapView;
		private readonly Dictionary<OverlayAlias, MKOverlayRenderer> _overlayRenderers = new Dictionary<OverlayAlias, MKOverlayRenderer>();
		private readonly SerialDisposable _elementsDisposable = new SerialDisposable();
		private readonly Dictionary<DependencyObject, MapControlAnnotation> _elements = new Dictionary<DependencyObject, MapControlAnnotation>();
		private bool _changingCenter;

		private MapControl? _owner;

		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");
			}

			_internalMapView = new MKMapView();

			_mapGrid.Children.Add(_internalMapView);
			_internalMapView.ShowsUserLocation = false;

			_internalMapView.RegionChanged += OnRegionChanged;

			OnChildrenCollectionChanged(this, null);

			SetUpOverlayRenderer();

View on GitHub (pinned to 0418340488)

Solutions

  1. Include <Grid x:Name="MapGrid"/> and <Grid x:Name="LayerGrid"/> in any custom MapControl template (mirror Generic.xaml).
  2. Avoid platform-specific template divergence unless you also duplicate the required parts per platform.
  3. Validate the applied template at runtime by enumerating the visual tree.
  4. Fall back to the default template to isolate whether the custom Style is the cause.

Example fix

// before — iOS custom template without MapGrid
<ControlTemplate><ContentPresenter/></ControlTemplate>

// after
<ControlTemplate>
  <Grid>
    <Grid x:Name="MapGrid"/>
    <Grid x:Name="LayerGrid"/>
  </Grid>
</ControlTemplate>
Defensive patterns

Strategy: validation

Validate before calling

var mapGrid = mapControl.GetTemplateChild("MapGrid") as Grid;
if (mapGrid == null) throw new InvalidOperationException("Custom template missing MapGrid (iOS)");

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 (iOS) template missing MapGrid part"); }

Prevention

When it happens

Trigger: A custom ControlTemplate on MapControl/MapPresenter lacks a Grid named 'MapGrid' when running on iOS; OnApplyTemplate cannot retrieve it before creating MKMapView is added to the visual tree.

Common situations: Sharing a custom MapControl Style across platforms that omits the template part; iOS-specific theme override missing the named Grid; template built by a design tool that stripped x:Name.

Related errors


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