WPF outsourcing Win8 outsourcing HTML5 outsourcing looking for Beijing dynamic point flying: WPF 4.5 probing six event tag Extension

Source: Internet
Author: User
This article is part of the new feature series of WPF 4.5. We will explore the "event tag extension" in WPF 4.5 ".

What is tag extension? In what scenarios?

As Rob points out, value can be provided for tag extensions in XAML. In this case, a delegate is also provided, and the WPF framework itself does not define the tag extension to be used for events.

In my opinion, this feature will use new scenarios. In fact, it is a dream for some people to create such an extension scenario, that is, to trigger an action when an event is triggered in space. In addition to remembering which namespace to add and use behavior and trigger, developers only need to create a tag extension, just as they will create a converter. Of course, there is one major drawback: Hybrid deployment is not supported. If you are also a fan of blend, you will continue to use behavior/trigger, because it only requires simple drag and drop. Therefore, many people think that it will not be out of date, which is still a good feature and choice to remember.

How to Use tag extension?

This time, it is obvious that it cannot be used like other features of WPF.

As a tag extension, a class inherits the tag extension and is abstracted as the providevalue method. This is the so-called framework. It provides an iserviceprovider object as a parameter.

This serviceprovider is a dependency parser. You can use it to obtain a service named iprovidevaluetarget, which is then used to obtain the target object of the property markupextension.

This attribute is the accessor of the event activity (it is called using the "+ =" syntax when you subscribe to it ). In this case, reflection is used to find the type of the handler for the target event.

Once this is done, a delegate and the returned values provided by the markupextension will be created. In the following example, the delegate handler is a method named "mymarkupextensioninternalhandler" and is defined in the tag extension 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 = 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 saved med.
}

Some things you must know when creating your tag extension:

1. When something bad happens, drop invalidoperationexception,

2. Do not think that everything has been initialized: Of course not, especially for the target data file.

3. Check the retrieved non-empty objects, especially the services obtained through the iserviceprovider parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.