{"record":{"id":"2e03d42f10da9dd9","repo":"PrismLibrary/Prism","slug":"resources-serviceproviderdidnothaveiprovidevaluetarget","errorCode":null,"errorMessage":"Resources.ServiceProviderDidNotHaveIProvideValueTarget","messagePattern":"Resources\\.ServiceProviderDidNotHaveIProvideValueTarget","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Xaml/TargetAwareExtensionBase.cs","lineNumber":78,"sourceCode":"\n    /// <summary>\n    /// Gets or sets the strategy for determining the binding context for the target.\n    /// </summary>\n    public TargetBindingContext TargetBindingContext { get; set; }\n\n    /// <summary>\n    /// Provides the value for the markup extension.\n    /// </summary>\n    /// <param name=\"serviceProvider\">The service provider for the markup extension.</param>\n    /// <returns>The value to set on the property where the extension is applied.</returns>\n    /// <exception cref=\"ArgumentException\">Thrown if the service provider does not provide an <see cref=\"IProvideValueTarget\"/>.</exception>\n    /// <exception cref=\"Exception\">Thrown if the target object is not supported.</exception>\n    T IMarkupExtension<T>.ProvideValue(IServiceProvider serviceProvider)\n    {\n        var valueTargetProvider = serviceProvider.GetService<IProvideValueTarget>();\n\n        if (valueTargetProvider == null)\n            throw new ArgumentException(Resources.ServiceProviderDidNotHaveIProvideValueTarget);\n\n        TargetElement = valueTargetProvider.TargetObject as VisualElement;\n\n        //this is handling the scenario of the extension being used within the EventToCommandBehavior\n        if (TargetElement is null && valueTargetProvider.TargetObject is BehaviorBase<BindableObject> behavior)\n            TargetElement = behavior.AssociatedObject as VisualElement;\n\n        if (TargetElement is null)\n            throw new Exception($\"{valueTargetProvider.TargetObject} is not supported\");\n\n        if (TargetElement.TryGetParentPage(out var page))\n            Page = page;\n        else\n            TargetElement.Behaviors.Add(new ElementParentedCallbackBehavior(() => Page = TargetElement.GetParentPage()));\n\n        return ProvideValue(serviceProvider);\n    }\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Xaml/TargetAwareExtensionBase.cs#L60-L96","documentation":"TargetAwareExtensionBase's IMarkupExtension<T>.ProvideValue asks the XAML service provider for IProvideValueTarget. When the service provider cannot supply it (it returns null), the extension throws an ArgumentException with the resource message, since the markup extension cannot determine the target object/property it should apply to.","triggerScenarios":"Using a Prism markup extension (e.g. x:Static-backed value extensions like OnPlatform/OnIdiom-style TargetAware extensions) in a context whose IServiceProvider does not expose IProvideValueTarget — e.g. invoking ProvideValue with a custom/foreign service provider, or use outside genuine XAML parsing.","commonSituations":"Calling ProvideValue manually in code or tests with a mock service provider lacking IProvideValueTarget; using the extension in non-XAML contexts; MAUI/XAML infrastructure changes where the service provider differs; misusing the extension inside styles/templates in ways the base didn't anticipate.","solutions":["Use the extension only within XAML parsed by MAUI, where IProvideValueTarget is always provided.","If calling ProvideValue programmatically/tests, supply a service provider that implements IProvideValueTarget (returning the target VisualElement and property).","Check the Prism.Maui version for known fixes to markup extension service provider handling and upgrade if affected.","If the extension must run in an unsupported context, compute the value directly instead of via ProvideValue."],"exampleFix":"// before: manual invocation without IProvideValueTarget\nvar value = ((IMarkupExtension<string>)new MyExtension()).ProvideValue(new MyServiceProvider());\n// after: provide the required service\nvar provider = new SimpleServiceProvider(typeof(IProvideValueTarget), new MyValueTarget(targetElement, targetProperty));\nvar value = ((IMarkupExtension<string>)new MyExtension()).ProvideValue(provider);","handlingStrategy":"type-guard","validationCode":"var target = serviceProvider.GetService(typeof(IProvideValueTarget));\nif (target is null) throw new InvalidOperationException(\"Use this extension only in XAML\");","typeGuard":"static bool SupportsMarkupExtension(IServiceProvider sp) =>\n    sp?.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget;","tryCatchPattern":"try\n{\n    value = extension.ProvideValue(serviceProvider);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"IProvideValueTarget\"))\n{\n    value = fallbackValue;\n}","preventionTips":["Only use TargetAware markup extensions inside real MAUI XAML.","In tests, provide a service provider stub that returns IProvideValueTarget.","Keep Prism.Maui updated for markup extension infrastructure fixes."],"tags":["dotnet","maui","prism","xaml","markup-extension"],"backgroundTag":"missing-dependency","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}