dotnet/maui · error · InvalidOperationException
ViewCell must have a View
Error message
ViewCell must have a View
What it means
ViewCellRenderer.GetNewRenderer throws InvalidOperationException when ViewCell.View is null. The renderer needs a View to create its child platform renderer via Platform.CreateRenderer. This fires during cell recycling when UpdateCell is called on a ViewCell that has no View content.
Source
Thrown at src/Compatibility/Core/src/iOS/Cells/ViewCellRenderer.cs:167
if (_viewCell != null)
{
_viewCell.PropertyChanged -= ViewCellPropertyChanged;
_viewCell.View.MeasureInvalidated -= OnMeasureInvalidated;
SetRealCell(_viewCell, null);
}
_viewCell = null;
}
_disposed = true;
base.Dispose(disposing);
}
IVisualElementRenderer GetNewRenderer()
{
if (_viewCell.View == null)
throw new InvalidOperationException($"ViewCell must have a {nameof(_viewCell.View)}");
var newRenderer = Platform.CreateRenderer(_viewCell.View);
_rendererRef = new WeakReference<IVisualElementRenderer>(newRenderer);
ContentView.AddSubview(newRenderer.NativeView);
return newRenderer;
}
void UpdateCell(ViewCell cell)
{
Performance.Start(out string reference);
var oldCell = _viewCell;
if (oldCell != null)
{
Device.BeginInvokeOnMainThread(oldCell.SendDisappearing);
oldCell.PropertyChanged -= ViewCellPropertyChanged;
oldCell.View.MeasureInvalidated -= OnMeasureInvalidated;
}View on GitHub (pinned to f377ff1c5e)
Solutions
- Ensure every ViewCell has a non-null View: <ViewCell><StackLayout>...</StackLayout></ViewCell>
- Validate DataTemplate output before returning: if (cell.View == null) cell.View = new ContentView();
- Use DataTemplateSelector implementations that always provide a complete ViewCell with View set
- Add a fallback View in DataTemplate creation code
Example fix
// before
var cell = new ViewCell(); // View is null
cell.SetBinding(...); // later throws when rendered
// after
var cell = new ViewCell
{
View = new StackLayout { Children = { new Label { Text = "Item" } } }
}; Defensive patterns
Strategy: validation
Validate before calling
// Before using a ViewCell, verify View is set
if (cell.View == null)
cell.View = new ContentView { Content = new Label { Text = "Loading..." } };
// Safe to render now Type guard
static bool HasView(ViewCell cell)
{
return cell?.View != null;
} Prevention
- Always set ViewCell.View in XAML: <ViewCell><ContentView>...</ContentView></ViewCell>
- Validate DataTemplate output in DataTemplateSelector implementations
- Provide fallback Views for all cell templates
- Unit test DataTemplates to ensure ViewCell.View is never null
When it happens
Trigger: A DataTemplate returns a ViewCell without setting the View property; a ViewCell in XAML with no child element; a cell template selector returns an incomplete ViewCell; programmatic ViewCell creation without View assignment.
Common situations: Empty or placeholder DataTemplate; conditional DataTemplateSelector that doesn't always set View; ViewCell defined in XAML with commented-out or missing content; ViewCell.View set to null during recycling.
Related errors
- Element must be a TabbedPage
- self
- renderer
- ViewCell must have a View
- Unexpected platform (expected: android) in device: {deviceDe
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/852440e71158a944.
Report an issue: GitHub.