练习 - 发现内在数据类型
假设您是 CRONUS International Ltd. 的开发人员,已经了解如何在 AL 中使用变量。 现在,您要练习如何声明和使用不同类型的变量。 您还希望了解如何在屏幕上显示变量值,以及各种数据类型的初始(默认)值是什么。
任务
创建新 AL 扩展。
新建卡页面。
创建全局变量。
在消息框中显示变量的值。
步骤
启动 Visual Studio Code。
选择视图 > 扩展 (Ctrl+Shift+X)。
在在市场中搜索扩展搜索框中输入 AL 语言。
选择绿色的安装按钮。
创建新 AL 扩展项目。 选择视图 > 命令面板... (Ctrl+Shift+P)。
在搜索框中输入 AL: Go!,然后从列表中选择命令。
接受建议的路径(或输入其他路径)。
选择 10.0 Business Central 2022 年发行版本第 2 波目标平台。
选择 Microsoft 云沙盒作为开发终结点。
下载应用程序符号。 选择视图 > 命令面板... (Ctrl+Shift+P)。
在搜索框中输入 AL: Download symbols,然后从列表中选择命令。
如果需要,请提供您的组织凭据(Microsoft 365 帐户/Microsoft Entra ID 帐户)。
打开 app.json 文件,将 name 设置更改为 DataTypes。 将 publisher 设置更改为 Cronus International Ltd。
删除 HelloWorld.al 文件。
选择文件 > 新建文件,然后通过选择文件 > 保存立即保存此文件。 将文件命名为 DataTypesCard.Page.al。
使用代码片段在此文件中创建一个新页面。 输入 tpage,然后按下拉列表中的第二个选项。
将 ID 更改为 50110,然后将 name 更改为 DataTypesCard。
验证 PageType 属性是否设置为 Card。
将 UsageCategory 属性设置为 Documents。
在 layout 部分中删除 field(Name; NameSource)。
在 actions 中删除 action(ActionName)。
将 PageType 属性设置为 Card,将 UsageCategory 属性设置为 Documents。
删除 SourceTable 属性,并将 Caption 属性设置为 Date Types Card。
创建以下全局变量,并删除默认的 myInt 变量。
LoopNo Integer
YesOrNo Boolean
Amount Decimal
"When Was It" Date
"What Time" Time
Description Text[30]
"Code Number" Code[10]
Ch Char
Color Option (values: Red, Orange, Yellow, Green, Blue, Violet)
在操作下,创建名为 OnOpenPage 的一个新触发器。
在 OnOpenPage 触发器的 begin 和 end 中输入以下代码。
Message('The value of %1 is %2','YesOrNo',YesOrNo); Message('The value of %1 is %2','Amount',Amount); Message('The value of %1 is %2','When Was It',"When Was It"); Message('The value of %1 is %2','What Time',"What Time"); Message('The value of %1 is %2','Description',Description); Message('The value of %1 is %2','Code Number',"Code Number"); Message('The value of %1 is %2','Ch',Ch); Message('The value of %1 is %2','Color',Color);
“DataTypesCard”页面代码现在应如下所示:
page 50110 DataTypesCard { PageType = Card; ApplicationArea = All; UsageCategory = Documents; Caption = 'Data Types Card'; layout { area(Content) { group(GroupName) { } } } actions { area(Processing) { } } trigger OnOpenPage() begin Message('The value of %1 is %2', 'YesOrNo', YesOrNo); Message('The value of %1 is %2', 'Amount', Amount); Message('The value of %1 is %2', 'When Was It', "When Was It"); Message('The value of %1 is %2', 'What Time', "What Time"); Message('The value of %1 is %2', 'Description', Description); Message('The value of %1 is %2', 'Code Number', "Code Number"); Message('The value of %1 is %2', 'Ch', Ch); Message('The value of %1 is %2', 'Color', Color); end; var LoopNo: Integer; YesOrNo: Boolean; Amount: Decimal; "When Was It": Date; "What Time": Time; Description: Text[30]; "Code Number": Code[10]; Ch: Char; Color: Option Red,Orange,Yellow,Green,Blue,Violet; }
打开 .vscode 文件夹中的 launch.json 文件。 将 startupObjectId 设置为 50110,将 startupObjectType 设置为 Page。
将扩展发布到沙盒。 选择视图 > 命令面板... (Ctrl+Shift+P)。
在搜索框中输入 AL: Publish(或者按 F5 键),然后从列表中选择命令。
验证 Dynamics 365 Business Central 应用程序是否会启动以及数据类型卡页面是否显示。 您会看到包含每个变量值的消息框。
您会看到包含每个变量值的消息框。
因为您还没有为变量分配任何值,所以显示默认值。