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

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 21.04.2017, 03:30   #1  
Blog bot is offline
Blog bot
Участник
 
25,459 / 846 (79) +++++++
Регистрация: 28.10.2006
stoneridgesoftware: Delegates in Dynamics 365 for Operations
Источник: https://stoneridgesoftware.com/deleg...or-operations/
==============

Dynamics 365 for Operations is well on its way to making over-layering of code a thing of the past. The application is moving towards and extension model approach. This approach should allow for a faster reaction to provide and push hotfixes and continuous upgrades. One approach Dynamics 365 for Operations is taking to make the application more extensible is the utilization of delegates. By adding these delegates, they are adding the ability for customers and/or partners to add customizations that can be stored in their own package.

A few things to know about x++ Delegates:

• Cannot have modifier. It is always protected.
• Return type must be “Void”
• Defining method cannot contain code
• Subscribing methods must have the same signature as delegate the delegate definition.

Below are a couple delegate examples for Dynamics 365 for Operations.

Create delegate definition

The following code shows an example of a delegate definition:

delegate void defineDelegate() { }
Create delegate handler

Open the class in the visual studio designer right click on the delegate definition and click “Copy event handler method” from the menu. The will automatically create the handler method for the delegate.



The following code shows example of a handler method.

[SubscribesTo(classStr(DelegateExample), delegateStr(DelegateExample, defineDelegate))] public static void DelegateExample_defineDelegate() { }
Full code example

Below I have provided a set of classes that show examples of different ways you can use delegates.

There are three classes:
1. DelegateExamples – Contains delegate definitions
2. DelegateExampleSubscriptions – Contains handler methods
3. DelegateExampleRun – Job that will run and product the output

DelegateExamples

/// /// Example of delegates. Dynamics for Opertions X++ /// class DelegateExamples { /// /// Delegate definition. /// /// delegate void addTwoNumbersDelegate(real a, real b) { } /// /// Delegate definition that provides a way to return a result from the delegate handler. /// /// delegate void addTwoNumbersReturnResultDelegate(real a, real b, EventHandlerResult result ) { } /// /// Call to the addTwoNumbersDelegate delegate, since actual call is protected. /// /// /// public void triggerAddTwoNumbersDelegate(real a, real b) { this.addTwoNumbersDelegate(a,b); } /// /// Uses delegate in a method which will return a result from the delegates handler. /// /// /// public real returnDelegateAdditionResults(real a, real b) { EventHandlerResult result = new EventHandlerResult(); real total = 0; this.addTwoNumbersReturnResultDelegate(a,b,result); total = result.result(); return total; } } DelegateExampleSubcriptions

/// /// Contains the delegate handler examples to the delegates created in the DelegateExample class. /// Dynamics for Operations X++ /// class DelegateExampleSubscriptions { /// /// Delegate handler. This is where to add your business logic to do something. /// For example insert/update values in a table. /// /// /// [SubscribesTo(classStr(DelegateExamples), delegateStr(DelegateExamples, addTwoNumbersDelegate))] public static void DelegateExample_addTwoNumbersDelegate(real a, real b) { real total = 0; total = a + b; info(strFmt("Invoking delegate addTwoNumbersDelegate: total %1", total)); } /// /// Delegate handler. This will allow you to return a result to the method that /// triggered the delegate. /// /// /// /// [SubscribesTo(classStr(DelegateExamples), delegateStr(DelegateExamples, addTwoNumbersReturnResultDelegate))] public static void DelegateExample_addTwoNumbersReturnResultDelegate(real a, real b, EventHandlerResult result) { real total = 0; total = a + b; result.result(total); } } DelegateExampleRun /// /// Class with main method /// class DelegateExampleRun { /// /// Runs the class with the specified arguments. /// /// The specified arguments. public static void main(Args _args) { DelegateExamples dExample = new DelegateExamples(); real totalResult = 0; dExample.triggerAddTwoNumbersDelegate(5,3); totalResult = dExample.returnDelegateAdditionResults(1.5,3.5); info(strFmt("Invoking delegate returnDelegateAdditionResults: total %1", totalResult)); } } Output



To Run:
• Copy all three classes into visual studio.
• Compile
• Right click on the DelegateExampleRun class and set as start up object as shown below.



• Then click start in visual studio to run





Источник: https://stoneridgesoftware.com/deleg...or-operations/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
stoneridgesoftware: Filtering Lookup by Relation in Dynamics 365 for Operations Blog bot DAX Blogs 0 14.03.2017 00:13
german_nav_developer: Buildnummern-Übersicht Microsoft Dynamics NAV 2013 Blog bot NAV: Blogs 0 15.05.2016 18:12
crminthefield: Podcast and Overview: Microsoft Dynamics CRM 2011 Update Rollup 15 Blog bot Dynamics CRM: Blogs 1 10.02.2016 10:26
crminthefield: Podcast and Overview: Microsoft Dynamics CRM 2011 Update Rollup 10 Blog bot Dynamics CRM: Blogs 0 17.08.2012 03:27
CRM DE LA CREME! Configuring Microsoft Dynamics CRM 4.0 for Internet-facing deployment Blog bot Dynamics CRM: Blogs 0 18.08.2009 11:05
Опции темы Поиск в этой теме
Поиск в этой теме:

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

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

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

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