I have an issue in xaml where a custom MarkupExtension's ProvideValue returns a string, but the xaml "compilation" doesn't know that and gives the following error: A key for a dictionary cannot be of type 'MyNamespace.MyExtension'. Only String, TypeExtension, and StaticExtension are supported. That error is unfortunate, since I know that the value provided will, in fact, be a string, which is an accepted type, as per the error message. Here is an example that reproduces the error:
Extension code:
[MarkupExtensionReturnType(typeof(String))]
public class MyExtension : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return "Hello World!";
}
}
Usage:
<sys:Double x:Key="{my:MyExtension}">99</sys:Double>
EDIT2: Simplified the question as I found after testing that it could be more specific
To fix your problem, you can make
MyExtensioninherit from eitherStaticExtensionorTypeExtension(both fromSystem.Windows.Markup). This doesn't look really clean sinceMyExtensionisn't related toTypeExtensionorStaticExtensionbut that's the only way I know of to bypass the compile time check that makes sure resource keys are only of typeString,TypeExtensionorStaticExtension.After inheriting from
TypeExtensionorStaticExtensionthere's nothing more to do than if you were inheriting directly fromMarkupExtension(so you only have to overrideProvideValue) with your current implementation.