WPF 4.5 – Part 6 : Markup Extensions for Events

, , 7 Comments

The first time I read about this new feature and its description, I just shake my head and thought : “what is this ?”! Then Rob Relyea, the ex-PM of the WPF team and Fabien Lavocat save my day via Twitter : “create a delegate in the provide value method“.

In this post, which is a part of a serie on the WPF 4.5 new features, we will discover what means exactly “Markup Extensions for Events“.

What is it and what scenario it addresses ?


As point out by Rob, markup extension can now provide values for event in the XAML. In this case, this is a Delegate which has to be provided. The WPF framework by itself does not define a markup extension to be used for events.

This feature enable new scenario which will, in my opinion, step on the Blend behaviors toes. Indeed, to be able to create this kind of markup extension is a dream for someone which want to trigger an action on a control when an event is raised. Instead of remember which namespace to add and which behavior to use with which trigger a developer will just have to create a markup extension just like he would create a converter.

Of course there is a drawback, a major one: this is not supported by Blend. If you are as a fan of Blend as I am, you’ll continue to use behavior/triggers because it’s simply a drag & drop ahead !

So even if, in my opinion it won’t be over-used, this is still a great feature and an option to keep in mind.

How to use it ?

This time, it is not as obvious to uses as the other new features of WPF 4.5.

To be a MarkupExtension, a class have to inherit from MarkupExtesion and to implement the abstract ProvideValue method. It is called by the framework which provide an IServiceProvider object as a parameter.

This serviceProvider is a dependency resolver which you can use to retrieve a service named IProvideValueTarget. This one will then be used to obtain the property targeted by the MarkupExtension(you can get the Targeted object too with it).

This property is the Event’s accessor (the one which is called when you subscribe to it with the ‘+=’ syntax). Then, reflection has to be used to find the Type of the handler of the aimed event.

Once this is done, a Delegate can be created and returned as a provided value by this MarkupExtension. In the example below, the delegate handler is a method named ‘MyMarkupExtensionInternalHandler’ defined on the MarkupExtension itself.

[csharp]public override object ProvideValue(IServiceProvider serviceProvider)
{
IProvideValueTarget targetProvider = serviceProvider
.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (targetProvider == null)
throw new InvalidOperationException(@"The CallAction extension
can’t retrieved the IProvideValueTarget service.");

var targetEventAddMethod = targetProvider.TargetProperty as MethodInfo;

//Retrieve the handler of the event
ParameterInfo[] pars = targetEventAddMethod.GetParameters();
Type delegateType = pars[1].ParameterType;

//Retrieves the method info of the proxy handler
MethodInfo methodInfo = this.GetType()
.GetMethod("MyMarkupExtensionInternalHandler",
BindingFlags.NonPublic | BindingFlags.Instance);

//Create a delegate to the proxy handler on the markupExtension
Delegate returnedDelegate = Delegate
.CreateDelegate(delegateType, this, methodInfo);

return returnedDelegate;
}

void MyMarkupExtensionInternalHandler(object sender, EventArgs e)
{
//here something can be performed.
}[/csharp]

A few good things to know when you create your own markup extensions:

  1. throw InvalidOperationException when something bad happens,
  2. don’t think that everything is initialized: it is not. This is especially true for the DataContext of the target,
  3. always checks that the objects you retrieve are not null especially the service obtained via the IServiceProvider argument.

More advanced example

I wanted to write a full example when I played with this feature yesterday but I found out it could be a post by itself. So if you want a full example, you can read this post describing how to invoke a method on the ViewModel / DataContext when an event is raised.;

Regards,

 

7 Responses

    • jmix90

      23/09/2011 11 h 03 min

      Great post, I didn't see it before !
      In the example for Silverlight it was EventInfo which was used too… This is really weird :-s !

Comments are closed.