AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
DAX
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 30.05.2018, 20:11   #1  
Blog bot is offline
Blog bot
Участник
 
21,999 / 765 (69) +++++++
Регистрация: 28.10.2006
sertandev: Accessing extension instance variables within Dynamics 365 FO form event handlers
Источник: http://devblog.sertanyaman.com/2018/...vent-handlers/
==============

To extend a form in D365 for Operations, we can use pre-defined events and form delegates. Event handler methods on a handler class can be assigned to them to be called when the event is triggered (For more info, see my previous post AX7 Extensibility – Part 3 : Event handlers and delegates (hooks)). As you know static methods can be called without creating a class instance and cannot access its class instance variables.

What if we need to access a class instance variable within these static event handlers?Like for example, the ‘counter’ variable below within the handler class?

final class TSExtensionInstanceTestHandler{ private int counter; [FormControlEventHandler(formControlStr(TSExtensionInstanceTest, Counter), FormControlEventType::Clicked)] public static void Toggle_OnClicked(FormControl sender, FormControlEventArgs e) { FormRun formRun = sender.formRun(); } [FormControlEventHandler(formControlStr(TSExtensionInstanceTest, Test), FormControlEventType::Clicked)] public static void Test_OnClicked(FormControl sender, FormControlEventArgs e) { FormRun formRun = sender.formRun(); }}
D365 fo forms have a possibility to register and recall extension class instances inside them. This way you can create a class instance, register it inside the formrun and access the instance in any of the static event handlers by calling “getExtensionInstance” method. Let’s do a simple example, creating a form with two buttons, “Counter” and “Test”, in which counter button will count and test button will display the counter value, and put all the logic in an event handler class.



first add a constructor to your class with caller formrun as a parameter :

public void new(FormRun _formRunInstance) { FormRun element = _formRunInstance;element.registerExtensionInstance(classStr(TSExtensionInstanceTestHandler), this); }Then add an init method to your event handler class:

private void init() { counter = 0; }Then we need to call our constructor and afterwards initialize our class instance, for that we use form’s ‘Initializing’ and ‘Initialized’ events :

[FormEventHandler(formStr(TSExtensionInstanceTest), FormEventType::Initializing)] public static void TSExtensionInstanceTest_OnInitializing(xFormRun sender, FormEventArgs e) { TSExtensionInstanceTestHandler extensionInstance = new TSExtensionInstanceTestHandler(sender); } [FormEventHandler(formStr(TSExtensionInstanceTest), FormEventType::Initialized)] public static void TSExtensionInstanceTest_OnInitialized(xFormRun sender, FormEventArgs e) { FormRun formRun = sender as FormRun; TSExtensionInstanceTestHandler extensionInstance = formRun.getExtensionInstance(classStr(TSExtensionInstanceTestHandler)); extensionInstance.init(); }Then we can use formRun’s “getExtensionInstance” method to access our class instance in our event handler methods:

public int increaseCounter() { counter++; return counter; } public int getCounter() { return counter; } [FormControlEventHandler(formControlStr(TSExtensionInstanceTest, Counter), FormControlEventType::Clicked)] public static void Counter_OnClicked(FormControl sender, FormControlEventArgs e) { FormRun formRun = sender.formRun(); TSExtensionInstanceTestHandler extensionInstance = formRun.getExtensionInstance(classStr(TSExtensionInstanceTestHandler)); extensionInstance.increaseCounter(); } [FormControlEventHandler(formControlStr(TSExtensionInstanceTest, Test), FormControlEventType::Clicked)] public static void Test_OnClicked(FormControl sender, FormControlEventArgs e) { FormRun formRun = sender.formRun(); TSExtensionInstanceTestHandler extensionInstance = formRun.getExtensionInstance(classStr(TSExtensionInstanceTestHandler)); info(strFmt("Counter value : %1", extensionInstance.getCounter())); }

Today instead of this construction, it is much better to use chain of command methods, but in case you want to use event handlers only, or update any older code, this is how you can use event handlers with class instance variables.

For more information and to see an example by Microsoft, you can check out the following AX class :

“HcmPositionFormExtensionPayrollHandler”

You can download the demo project for this blog post from the following GitHub link :

https://github.com/sertanyaman/Serta...tanceTest.axpp









Источник: http://devblog.sertanyaman.com/2018/...vent-handlers/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
atinkerersnotebook: Creating a new Vendor Registration Form For Dynamics 365 Using Office Forms and Flow Blog bot DAX Blogs 0 22.03.2018 23:16
sertandev: AX7 Extensibility – Part 3 : Event handlers and delegates (hooks) Blog bot DAX Blogs 0 20.03.2018 13:11
survivingcrm: Trial & Error: Understanding Dynamics 365 CE Trials Blog bot Dynamics CRM: Blogs 0 26.02.2018 20:11
crminthefield: Podcast and Overview: Microsoft Dynamics CRM 2011 Update Rollup 16 Blog bot Dynamics CRM: Blogs 0 23.01.2014 03:15
CRM DE LA CREME! Some more useful javascripts for MS CRM Blog bot Dynamics CRM: Blogs 0 04.05.2010 11:05
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 10:02.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.