unoplatform/uno · error · InvalidOperationException

Unable to find [LayerGrid] template part

Error message

Unable to find [LayerGrid] template part

What it means

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.

Source

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

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

		private void OnMapReady(GoogleMap map)
		{
			_map = map;
			_map.MyLocationEnabled = false;

View on GitHub (pinned to 0418340488)

Solutions

  1. Ensure the template contains <Grid x:Name="LayerGrid"/> of type Grid exactly.
  2. Copy the default template from Generic.xaml and only restyle visuals, preserving both named Grid parts.
  3. Verify the element type — using a Canvas or Border named 'LayerGrid' will fail the 'as Grid' cast.
  4. Remove the custom Style override to confirm the default template works, then reintroduce changes incrementally.

Example fix

// before — LayerGrid is a Canvas (wrong type)
<Canvas x:Name="LayerGrid"/>

// after — correct type
<Grid x:Name="LayerGrid"/>
Defensive patterns

Strategy: validation

Validate before calling

var layerGrid = mapControl.GetTemplateChild("LayerGrid") as Grid;
if (layerGrid == null) throw new InvalidOperationException("Custom template missing LayerGrid");

Type guard

bool TemplateHasLayerGrid(Control c)
    => c.GetTemplateChild("LayerGrid") is Grid;

Try / catch

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

Prevention

When it happens

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

Common situations: Restyling MapControl and replacing LayerGrid with a different panel type; copy-paste template that renames the element; merging an incomplete theme resource dictionary.

Related errors


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