Walkthrough: Subscribing an Event Handler to a Delegate in the AOT
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
In Microsoft Dynamics AX you can use the AOT to make a method be an event handler for a delegate.
The tasks in this topic are as follows:
Add a delegate to a class
Add an event handler to a class
Add a public method that calls the delegate
Run a job that calls the public method
Prerequisites
To complete the tasks in this walkthrough you need to understand the following topics:
Add a Delegate to a Class
In the AOT, you can add a delegate to a class. Do as follows:
In the AOT, create a class named TestDelegateClass.
Right-click the TestDelegateClass node and then click New > Delegate.
Right-click the delegate node, and then click Edit. This starts the X++ Editor window.
In the Editor window, name the delegate reportColorDelegate.
In the Editor window, make the delegate input one parameter of type str.
Save the delegate.
Add an Event Handler to a Class
You can add a method to a class, and then you can subscribe the method to a delegate. Do as follows:
Right-click the TestDelegateClass node and then click New > Method.
Right-click the method node, and then click Edit.
In the Editor window, name the method reportColorEH.
In the Editor window, make the modifiers be static public.
Make the input parameters be the same as for the earlier delegate.
Here is an example of what the X++ code should look like.
static public void reportColorEH(str _color)
{
info(strFmt("%1 is the color value in the event handler.",_color));
}
Subscribe the Method to the Delegate as an Event Handler
You can subscribe the reportColorEH method to the reportColorDelegate delegate as an event handler. Do as follows:
Highlight the node for the reportColorEH method.
Use the mouse to drop the reportColorEH node onto the reportColorDelegate delegate node.
Add a public Method that Calls the Delegate
You can add a public method that calls the delegate. The X++ code in the method must look similar to the following code example.
public void callTheDelegate()
{
this.reportColorDelegate("red");
}
Run a Job that Calls the public Method
You can write and run a job that calls the public method that calls the delegate. Do as follows:
In the AOT, right-click the node AOT > Jobs, and then click New job.
In the code Editor window, enter X++ code that is similar to the following.
static void Job1(Args _args)
{
TestDelegateClass myTestDelegateClass = new TestDelegateClass();
myTestDelegateClass.callTheDelegate();
}
Compile the job by pressing the F1 key while focus is in the Editor window.
Run the job by pressing the F5 key.
The output to the Infolog window looks like the following:
red is the color value in the event handler.
See also
Event Handler Nodes in the AOT
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.