{"record":{"id":"369b6f77b1c9f291","repo":"dotnet/maui","slug":"rootcomponent-requires-a-value-for-its-selector-pr-369b6f","errorCode":null,"errorMessage":"RootComponent requires a value for its Selector property, but no value was set.","messagePattern":"RootComponent requires a value for its Selector property, but no value was set\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/BlazorWebView/src/Wpf/RootComponent.cs","lineNumber":40,"sourceCode":"\t\t/// <summary>\n\t\t/// Gets or sets the type of the root component. This type must implement <see cref=\"IComponent\"/>.\n\t\t/// </summary>\n\t\tpublic Type ComponentType { get; set; } = default!;\n\n\t\t/// <summary>\n\t\t/// Gets or sets an optional dictionary of parameters to pass to the root component.\n\t\t/// </summary>\n\t\tpublic IDictionary<string, object?>? Parameters { get; set; }\n\n\t\tinternal Task AddToWebViewManagerAsync(WebViewManager webViewManager)\n\t\t{\n\t\t\t// As a characteristic of XAML,we can't rely on non-default constructors. So we have to\n\t\t\t// validate that the required properties were set. We could skip validating this and allow\n\t\t\t// the lower-level renderer code to throw, but that would be harder for developers to understand.\n\n\t\t\tif (string.IsNullOrWhiteSpace(Selector))\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException($\"{nameof(RootComponent)} requires a value for its {nameof(Selector)} property, but no value was set.\");\n\t\t\t}\n\n\t\t\tif (ComponentType is null)\n\t\t\t{\n\t\t\t\tthrow new InvalidOperationException($\"{nameof(RootComponent)} requires a value for its {nameof(ComponentType)} property, but no value was set.\");\n\t\t\t}\n\n\t\t\tvar parameterView = Parameters == null ? ParameterView.Empty : ParameterView.FromDictionary(Parameters);\n\t\t\treturn webViewManager.AddRootComponentAsync(ComponentType, Selector, parameterView);\n\t\t}\n\n\t\tinternal Task RemoveFromWebViewManagerAsync(WebView2WebViewManager webviewManager)\n\t\t{\n\t\t\treturn webviewManager.RemoveRootComponentAsync(Selector);\n\t\t}\n\t}\n}\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/BlazorWebView/src/Wpf/RootComponent.cs#L22-L58","documentation":"The WPF RootComponent.AddToWebViewManagerAsync validates Selector is non-empty before calling AddRootComponentAsync on the WebView2WebViewManager. This mirrors the MAUI RootComponent pattern: since WPF allows XAML instantiation with parameterless constructors, properties are validated at add-time rather than construction-time. A null Selector means there is no DOM insertion point for the component.","triggerScenarios":"A WPF RootComponent with an unset or whitespace Selector has AddToWebViewManagerAsync called — either explicitly or when the BlazorWebView collects RootComponents during its initialization pass.","commonSituations":"XAML RootComponent declaration missing the Selector attribute or bound to a null value; dynamically creating RootComponent objects and forgetting to set Selector; property binding that resolves to null at runtime; copy-paste error where Selector was removed during refactoring.","solutions":["Set the Selector property on the RootComponent before adding it: rc.Selector = \"#app\";","In XAML: <blazor:RootComponent Selector=\"#app\" ComponentType=\"{x:Type local:MyComponent}\" />.","If Selector comes from a binding, ensure the source property is non-null: use FallbackValue or TargetNullValue.","Validate dynamically created RootComponents before adding to the collection."],"exampleFix":"// before (XAML)\n<blazor:RootComponent ComponentType=\"{x:Type local:MyComponent}\" />\n// after\n<blazor:RootComponent Selector=\"#app\" ComponentType=\"{x:Type local:MyComponent}\" />","handlingStrategy":"validation","validationCode":"// Validate Selector before adding\nif (string.IsNullOrWhiteSpace(rootComponent.Selector))\n{\n    throw new InvalidOperationException(\"RootComponent.Selector must be set.\");\n}\nawait rootComponent.AddToWebViewManagerAsync(webViewManager);","typeGuard":"static bool IsValidWpfRootComponent(RootComponent rc)\n{\n    return !string.IsNullOrWhiteSpace(rc?.Selector);\n}","tryCatchPattern":null,"preventionTips":["Set Selector in XAML explicitly or via a non-null binding source.","Use FallbackValue on bindings to provide a default selector.","Validate RootComponent properties in a helper before adding to the collection.","Review XAML for RootComponent tags missing the Selector attribute."],"tags":["blazor","blazorwebview","wpf","rootcomponent","validation","xaml"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}