Skip to content

ServiceProvider and XAML: type converters and markup extensions

February 15, 2007

Internal Mail thread (read from bottom)…shared to make the info spread fastest…

Larry-
Thanks!

Wolf-
Not sure how well I’ve speced/documented this.  Can you please add to SDK?

Basically, we should make sure that we document that service provider is available inside a type converter ConvertFrom call and inside ProvideValue.

We provide 3 different interfaces that you can GetService from the service provider.

Here are the concrete examples of how we use this mechanism in our code base…(examples are simplified a bit)

Example of using IProvideValueTarget – similar to how Binding does this:

public sealed override object ProvideValue(IServiceProvider serviceProvider)
{
      if (serviceProvider != null)
      {
            IProvideValueTarget target1 = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            Object obj1 =      target1.TargetObject;
            Object property1 = target1.TargetProperty;  //might be a propertyInfo or DependencyProperty

Example of using IXamlTypeResolver inside a ProvideValue call for TypeExtension (supports x:Type)

public override object ProvideValue(IServiceProvider serviceProvider)
{
            string typeName = "foo:Button";
            IXamlTypeResolver resolver1 = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
            if (resolver1 == null)
            {
                  throw …
            }
            Type type = resolver1.Resolve(typeName);
            if (type == null)
            {
                  throw …
            }
      }
      return type;

}

Example of using IUriContext from inside a TypeConverter called during XamlReader or Baml load.

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
            IUriContext context1 = (IUriContext) context.GetService(typeof(IUriContext));
            if (context1.BaseUri != null)
            {
                    Uri baseUri = context1.BaseUri

From: Larry

Rob, as far as I can see this isn’t documented in MSDN. MSDN documents that MarkupExtension.ProvideValue takes an IServiceProvider, but doesn’t say what it is or does. It also documents IProvideValueTarget, but says only (cryptically, in my opinion): “Reports object-property relationships for framework features such as binding on a dependency property”. It also warns you off with this: “This interface is used internally by some WPF types, and should not be used by application or type authors.”

If third parties are supposed to be able to implement markup extensions (are they, by the way?) this would need to be better documented.

From → XAML (non-UI)

One Comment
  1. Denis permalink

    Thanks for the good post. The information is really valuable.
    One of the things we (developers) lack a lot in this case is gaining access to service provider or injecting additional stuff there. Would be really great having possibility attaching to an existing service bus I think.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: