使用Management Host Control

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.

    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. Right click on Forms and create a new Form ManagedHostDemo

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

  4. 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
  5. Select System.Windows.Forms from the assembly list and DataGridView from the control list and press OK

  6. A new managed host control will be added. Rename the control to DataGridViewManagedHost

     

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

     

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

     

  9. Open the Class declaration node and write the following code
    //declare a variable of datagridview type
    System.Windows.Forms.DataGridView dataGridView;
    

     

  10. Override the init method of the form and write the following code in it after the super() call
    //get the reference of the managed host control
    dataGridView = DataGridViewManagedHost.control();
    //set row header visible property to false
    dataGridView.set_RowHeadersVisible(false);
    //set columns to auto fill
    dataGridView.set_AutoSizeColumnsMode(System.Windows.Forms.DataGridViewAutoSizeColumnsMode::Fill);
    

     

  11. Override the run method of the form and write the following code
    public void run()
    {
        System.Windows.Forms.DataGridViewColumnCollection   columnCollection;
        System.Windows.Forms.DataGridViewRowCollection      rowCollection;
        CustTable custTable;
        System.String[] strValues;
        
        super();
        
        //define the columns of the grid
        columnCollection = dataGridView.get_Columns();
        columnCollection.Add("CustomerAcount","Customer account");
        columnCollection.Add("CustomerName","Customer name");
        
        //fill the rows of the grid
        rowCollection = dataGridView.get_Rows();
        
        while select * from custTable
        {
            strValues = new System.String[2]();
            
            strValues.set_Item(0, custTable.AccountNum);
            strValues.set_Item(1, custTable.name());
            
            rowCollection.Add(strValues);  
        }
    }
    

     

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

     

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

     

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

     

  15. Right click the DataGridViewManagedHost control and select Events

     

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

     

  17. Select SelectionChanged event and click on Add button

     

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

     

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

     

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

     

  21. Now open the DataGridViewManagedHost_SelectionChanged method and write the following code in it
    void DataGridViewManagedHost_SelectionChanged(System.Object sender, System.EventArgs e)
    {
        System.Windows.Forms.DataGridViewCell cell;
        System.Windows.Forms.DataGridViewRow row = dataGridView.get_CurrentRow();
        
        cell = dataGridView.get_Item(0,row.get_Index());    
        info(cell.get_Value());
    }
    

     

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值