The markup extension uses the IXamlTypeResolver
service to convert a TypeName string to a CLR Type. But in design mode, IServiceProvider
does not return this service. Because of this, errors occur in the XAML Editor window and the Designer does not work.
Are there ways to create an IXamlTypeResolver
or other ways to convert a TypeName string to a CLR Type?
Maybe there is a possibility to get ITypeDescriptorContext
for TypeConverter?
An example of the simplest implementation. Everything except the problem discussed here is excluded from the example.
public class SomeCustomExtension : MarkupExtension
{
public string TypeName { get; set; } = string.Empty;
public SomeCustomExtension(string typeName)
{
TypeName = typeName;
}
public SomeCustomExtension()
{
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (serviceProvider.GetService(typeof(IXamlTypeResolver)) is not IXamlTypeResolver typeResolver)
throw new InvalidOperationException("No service \"IXamlTypeResolver\".");
return typeResolver.Resolve(TypeName);
}
}
<TextBlock Text="{Binding Tag, RelativeSource={RelativeSource Self}, TargetNullValue=null}"
Tag="{local:SomeCustom sys:Double}"/>
P.S. A practical application uses a much more complex markup extension implementation. And its full work for the Design Mode is important because it greatly facilitates the Development of XAML layout.