Using Managed Host Control in Microsoft Dynamics AX 2012

Overview

Managed host control in Microsoft Dynamics AX 2012 is used to host a .NET or WPF control on a Microsoft Dynamics AX form. It is used to enhance the usability of the application and provides users with a rich experience design.

The ability to add these controls on a form gives access to many features that are not natively supported by standard Microsoft Dynamics AX. In addition, customized controls can be created in .NET and used as a managed host control. The integration between Microsoft Dynamics AX and .NET controls is seamless and easy to implement. Managed host controls do not support Microsoft Silverlight controls, however, Silverlight controls can be used in the Enterprise portal.

Managed host control uses the CLR interop feature of X++ to access the properties, methods and events of a specified .NET or WPF control. CLR interop is used because it supports implicit conversion of many X++ data types into corresponding .NET CLR data types. For complex data types like collections, some conversion may have to be performed.

Pre-requisites

  1. Microsoft Dynamics AX 2012
  2. The assembly of the control to be used must be added as a reference in the Microsoft Dynamics AX references node (AOT à References) and the assembly must be installed on the computer where Microsoft Dynamics AX client is installed

Important Concepts

  1. Properties

    Properties are used to assign or retrieve the values that define the behavior of a control. Since Microsoft Dynamics AX does not supports properties, all the properties of a .NET control are converted into methods. To assign a property any value, set_<Property Name>() is used and to retrieve a property’s value, where get_<Property Name()> is used.

  2. Events

    Event refers to any activity or occurrence detected by the system in response to a user or a system action. Both X++ and .NET controls support events. To integrate the events of a .NET control in Microsoft Dynamics AX, event handlers are used. An event handler is a registered form method that gets called every time an event is raised by the system.

  3. To register a .NET control event on a managed host control, a reference of the managed H=host control is obtained from an instance of a .NET control. The instance of the .NET control contains methods with the name add_<Event Name>, which is used to register a .NET event handler in Microsoft Dynamics AX. Microsoft Dynamics AX contains a class ManagedEventHandler whose instance is passed to the add_<Event Name> method.

    To register a form method as an event handler, pass the instance of ManagedEventHandler class, which takes the form and name of the method as an input, to the event method of .NET control as a parameter. The necessary business logic is added to the form method, which gets called every time the .NET control raises the event. To unregister an event handler, use the remove_<Event Name> method of the .NET control.

Scenario

As part of this tutorial, all customers will be listed in a .NET grid view control and the selected record of the grid will be shown in an infolog.

Steps

  1. First of all create a new form. Open AOT à Forms
  2.  

  3. Right click on Forms and create a new Form ManagedHostDemo

  4. Navigate to the Design node of the form and add a ManagedHost control.

  5. The Managed control selector dialog will then open. On the dialog, the top grid lists all the assemblies that are referenced in Microsoft Dynamics AX. To reference a new assembly, the Add reference button is used. The below grid list all the controls available in the selected assembly
  6. Select System.Windows.Forms from the assembly list and DataGridView from the control list and press OK
  7. A new managed host control will be added. Rename the control to DataGridViewManagedHost
  8.  

  9. Set the Height property to Column height and Width property to Column width of the control
  10.  

  11. Since managed host control cannot be used directly to access the methods of a .NET control, the reference of the managed host control must be obtained in a global variable of the same type as .NET control so that different properties and methods can be accessed
  12.  

  13. Open the Class declaration node and write the following code

  14.  

  15. Override the init method of the form and write the following code in it after the super() call
  16.  

  17. Override the run method of the form and write the following code

  18.  

  19. Now open the form by right clicking the form and select Open

  20.  

  21. All the customers will be shown in the .NET grid view control
  22.  

  23. Now an event will be registered that will show the selected record in an infolog
  24.  

  25. Right click the DataGridViewManagedHost control and select Events
  26.  

  27. The Events dialog is then opened. It lists all the events that are available for the control
  28.  

  29. Select SelectionChanged event and click on Add button
  30.  

  31. It will create a corresponding X++ method and will register the method as an event handler in the init method
  32.  

  33. The name of the newly created method is shown in the X++ methods column. Press the Close button to close the form
  34.  

  35. Navigate to the init method and verify that the method added above is registered as an event handler. Since SelectionChanged event was to be registered, it is accessed via add_SelectionChanged method of the .NET control. An instance of ManagedEventHandler class having form and method name as input is passed as a parameter to the method
  36.  

  37. Now open the DataGridViewManagedHost_SelectionChanged method and write the following code in it

  38.  

  39. Open the form and select any row. The selected customer account will be shown in the infolog

March 5, 2015