Code behind extension forms: How to add state variables and override methods without overlayering
The August 2016 release of the new Dynamics AX platform (aka platform update 2 or platform 7.2) enables developers to define form extension classes. Why is this feature useful? In previous releases of the new Dynamics AX, developers could extend form metadata and author form event handlers, but there was no simple way to define a new form variable and store a form state to use across different event handlers.
This articles gives an example on how to add a state to a form extension and how to use a form extension class to override a form method without overlayering the source code of the form. I hope you find it useful.
Override a method using a form extension class
I will start with an example showing how to override a method in the form extension class.
Consider the following scenario:
Consider the following scenario:
- A form named Form1.
- A data source in Form1 named Table1 and referencing the table Table1.
- A field in the data source named Field1.
A developer who needs to override the JumpRef method for field1 can user a form extension class as follows.
[ExtensionOf(formstr(Form1))]
final class Form1_Extension // This name can be anything, the only condition is that it ends with the _Extension suffix, no need to name it Form1
{
[FormEventHandler( formstr( Form1 ) FormEventType::Initialized)]
public void initializedFormHandler(xFormRun formRun, FormEventArgs e)
{
FormDataObject field1DataObject = this.table1_ds.object( fieldNum(Table1, field1 ), 1);
field1DataObject.registerOverrideMethod( methodStr(FormDataObject, jumpRef), formMethodStr( Form1, jumpRefImplementation), this);
}
public void jumpRefImplementation(FormDataObject dataSourceField1)
{
// Your implementation of the override logic
}
}
Add a state to a form extension
In the scenario above, what if the developer needs to add a state to the existing Form1, but does not want to edit or overlayer the form's source code? In the Form1_Extension class, the developer can add member variables and these variables will be associated with the Form1 instance. In the following example, var1 and var2 integers are added as state variables.
[ExtensionOf( formstr( Form1 ) ]
final class Form1_Extension
{
public int var1;
public int var2;
/*
[ExtensionOf( formstr( Form1 ) ]
final class Form1_Extension
{
public int var1;
public int var2;
/*
These variables can now be referenced from any instance method in the extension class. In order to complete this picture, imagine Form1 has one button, named Button1. You can implement in the same Form1_Extension extension class an event handler as follows.
*/
*/
[FormControlEventHandler(formControlStr(Form1, Button1), FormControlEventType::Clicked)]
public void onMatchNo_Clicked(FormControl sender, FormControlEventArgs e)
{
var1 = 10;
var2 =20;
}
}
As you can see, the event handler has not been defined as a static method (it is an instance method). This makes it possible for the handler to reference any state (variable) defined in the extension class. Another important aspect is that an instance method in a form extension class can make a reference to any of the public member variables, controls and data source objects defined on the form directly using the reference to “this”.
As you can see, the event handler has not been defined as a static method (it is an instance method). This makes it possible for the handler to reference any state (variable) defined in the extension class. Another important aspect is that an instance method in a form extension class can make a reference to any of the public member variables, controls and data source objects defined on the form directly using the reference to “this”.
Comments
Post a Comment