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 (iOS) when GetTemplateChild("LayerGrid") does not return a Grid. Same required template-part contract as the Android LayerGrid (error 27); the iOS presenter needs it for overlay layers.
Source
Thrown at src/AddIns/Uno.UI.Maps/MapPresenter.iOS.cs:53
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();
_internalMapView.GetViewForAnnotation = OnGetViewForAnnotation!;
}
private IDisposable UpdateOwnerSubscriptions(MapControl owner)
{View on GitHub (pinned to 0418340488)
Solutions
- Ensure <Grid x:Name="LayerGrid"/> (type Grid) is present in the template on iOS.
- Mirror the default Generic.xaml template and only restyle non-structural elements.
- Confirm element type is Grid, not Canvas/Border.
- Revert to default template to confirm root cause is the custom template.
Example fix
// before <Border x:Name="LayerGrid"/> // after <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 (iOS)"); 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 (iOS) template missing LayerGrid part"); } Prevention
- Keep the LayerGrid Grid template part in iOS custom templates.
- Ensure element type is Grid, not Canvas/Border.
- Validate against the default template.
When it happens
Trigger: Custom ControlTemplate for MapControl on iOS omits the 'LayerGrid' Grid or uses a non-Grid element named 'LayerGrid', causing the 'as Grid' cast to return null.
Common situations: Cross-platform shared Style that defines LayerGrid as a different panel; theme override dropping the named element; renaming during refactoring.
Related errors
- Unable to find [MapGrid] template part
- Unable to find [MapGrid] template part
- Unable to find [LayerGrid] template part
- Failed to load animation.
- Failed load the animation
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/68529239ad7820f5.
Report an issue: GitHub.