dotnet/maui · error · InvalidOperationException
RootComponent requires a value for its Selector property, bu
Error message
RootComponent requires a value for its Selector property, but no value was set.
What it means
The Maui RootComponent.AddToWebViewManagerAsync method validates that the Selector CSS property is non-empty before forwarding the request to WebViewManager.AddRootComponentAsync. Because XAML instantiates objects via parameterless constructors, required properties must be validated at call time rather than constructor time. A null or whitespace Selector means Blazor has no DOM target to render the component into.
Source
Thrown at src/BlazorWebView/src/Maui/RootComponent.cs:41
/// <summary>
/// Gets or sets the type of the root component. This type must implement <see cref="IComponent"/>.
/// </summary>
public Type? ComponentType { get; set; }
/// <summary>
/// Gets or sets an optional dictionary of parameters to pass to the root component.
/// </summary>
public IDictionary<string, object?>? Parameters { get; set; }
internal Task AddToWebViewManagerAsync(WebViewManager webViewManager)
{
// As a characteristic of XAML,we can't rely on non-default constructors. So we have to
// validate that the required properties were set. We could skip validating this and allow
// the lower-level renderer code to throw, but that would be harder for developers to understand.
if (string.IsNullOrWhiteSpace(Selector))
{
throw new InvalidOperationException($"{nameof(RootComponent)} requires a value for its {nameof(Selector)} property, but no value was set.");
}
if (ComponentType is null)
{
throw new InvalidOperationException($"{nameof(RootComponent)} requires a value for its {nameof(ComponentType)} property, but no value was set.");
}
var parameterView = Parameters == null ? ParameterView.Empty : ParameterView.FromDictionary(Parameters);
return webViewManager.AddRootComponentAsync(ComponentType, Selector, parameterView);
}
internal Task RemoveFromWebViewManagerAsync(WebViewManager webviewManager)
{
if (string.IsNullOrWhiteSpace(Selector))
{
throw new InvalidOperationException($"{nameof(RootComponent)} requires a value for its {nameof(Selector)} property, but no value was set.");
}
return webviewManager.RemoveRootComponentAsync(Selector);View on GitHub (pinned to f377ff1c5e)
Solutions
- Set the Selector property on every RootComponent to a valid CSS selector string (e.g. '#app' or '.blazor-root').
- In XAML, add the Selector attribute: <RootComponent Selector="#app" ComponentType="{x:Type local:MyComponent}" />.
- If creating RootComponents dynamically, validate Selector before adding: if (!string.IsNullOrWhiteSpace(rc.Selector)) { ... }.
- Audit all RootComponent declarations (XAML and code) to ensure none are missing Selector.
Example fix
// before
var rc = new RootComponent
{
ComponentType = typeof(MyComponent)
};
// after
var rc = new RootComponent
{
Selector = "#app",
ComponentType = typeof(MyComponent)
}; Defensive patterns
Strategy: validation
Validate before calling
// Validate before adding to WebViewManager
if (string.IsNullOrWhiteSpace(rootComponent.Selector))
{
throw new InvalidOperationException("RootComponent.Selector must be set before adding.");
}
await rootComponent.AddToWebViewManagerAsync(webViewManager); Type guard
static bool IsValidRootComponentSelector(RootComponent rc)
{
return !string.IsNullOrWhiteSpace(rc?.Selector);
} Prevention
- Set Selector in XAML attributes explicitly: <RootComponent Selector="#app" ... />.
- When creating RootComponent dynamically, always set Selector first.
- Use a helper method that validates Selector and ComponentType before adding.
- Add a code review checklist item for RootComponent property completeness.
When it happens
Trigger: A RootComponent declared in XAML or code has its Selector property unset (null or whitespace string) and then AddToWebViewManagerAsync is called, either explicitly or when the BlazorWebView collects its RootComponents collection during startup.
Common situations: XAML RootComponent tag missing the Selector attribute; dynamically creating RootComponent objects in a loop and forgetting to set Selector on one; copy-paste error where Selector was renamed or removed; binding Selector to a property that evaluates to null at runtime.
Related errors
- RootComponent requires a value for its ComponentType propert
- RootComponent requires a value for its Selector property, bu
- RootComponent requires a value for its ComponentType propert
- 'selector' cannot be null or whitespace.
- There is no root component with selector '{selector}'.
AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13).
Data as JSON: /api/errors/2c18a339ef377d23.
Report an issue: GitHub.