DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

WPF 4.5 – Part 6 : Markup Extensions for Events

Jon Antoine user avatar by
Jon Antoine
·
Aug. 13, 12 · Interview
Like (0)
Save
Tweet
Share
4.24K Views

Join the DZone community and get the full member experience.

Join For Free

MarkupExtension00The first time I read about this new feature and its description, I just shook 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 series 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 pointed 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, steps on the Blend behavior's 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 remembering 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 has 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.

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.
}

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 check 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.;

Windows Presentation Foundation Event

Published at DZone with permission of Jon Antoine, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes vs Docker: Differences Explained
  • How Observability Is Redefining Developer Roles
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: