Select File > New >Project… (shortcut: Ctrl + Shift + N). The New Project window is displayed.
从“ 语言” 下拉菜单中选择“ C# ”。 系统将显示使用 C# 的所有依赖项的列表。
Select Class Library (.NET Framework). This helps us export the custom activity as a .dll file. Select Next to go to the Configure your new project window.
Fill in the Project name field with the desired activity name. In our case, we can use "MathSquareOfSum".
从“框架 ”下拉菜单中选择“ .NET Framework 4.6.1 ”。 这可确保该库与 UiPath Studio 兼容。
注意: 在 Windows - 旧版项目中,UiPath Studio 支持使用 .NET Framework 4.5.2 至 4.6.1 创建的活动。 有关如何将活动迁移到 .NET 以在具有 Windows 兼容性的项目中使用的信息,请参阅将 活动迁移到 .NET 6。
Select Create to go to the code designer and start writing the activity code.
using System.Activities;
using System.ComponentModel;using System.Activities;
using System.ComponentModel;
我们的项目现在应如下所示:
添加输入和输出参数。 在本例中,代码应如下所示:
//Note that these attributes are localized so you need to localize this attribute for Studio languages other than English//Dots allow for hierarchy. App Integration.Excel is where Excel activities are.[Category("Category.Where.Your.Activity.Appears.In.Toolbox")][DisplayName("Human readable name instead of class name")][Description("The text of the tooltip")]publicclassMathSqSum: CodeActivity
{//Note that these attributes are localized so you need to localize this attribute for Studio languages other than English[Category("Input")][DisplayName("First Number")][Description("Enter the first number")][RequiredArgument]public InArgument<int> FirstNumber { get; set;}[Category("Input")][DisplayName("Second Number")][Description("Enter the second number")][RequiredArgument]public InArgument<int> SecondNumber { get; set;}[Category("Output")]public OutArgument<int> ResultNumber { get; set;}protected override voidExecute(CodeActivityContext context){}//Note that these attributes are localized so you need to localize this attribute for Studio languages other than English
//Dots allow for hierarchy. App Integration.Excel is where Excel activities are.
[Category("Category.Where.Your.Activity.Appears.In.Toolbox")]
[DisplayName("Human readable name instead of class name")]
[Description("The text of the tooltip")]
public class MathSqSum : CodeActivity
{
//Note that these attributes are localized so you need to localize this attribute for Studio languages other than English
[Category("Input")]
[DisplayName("First Number")]
[Description("Enter the first number")]
[RequiredArgument]
public InArgument<int> FirstNumber { get; set; }
[Category("Input")]
[DisplayName("Second Number")]
[Description("Enter the second number")]
[RequiredArgument]
public InArgument<int> SecondNumber { get; set; }
[Category("Output")]
public OutArgument<int> ResultNumber { get; set; }
protected override void Execute(CodeActivityContext context)
{
}
<DisplayName(" ")> 属性是 Studio 中“ 属性 ”面板中输入字段之前显示的标签。 <Description(" ")> 属性是鼠标悬停时显示的工具提示文本。
The <ReguiredArgument> attribute is needed if you want a declared element to be mandatory for input. If it is not filled in, a caution icon appears in the title bar of the activity.
Add the functionality in the <Execute( )> overridden function. In our case, it looks like this:
protected override voidExecute(CodeActivityContext context){var firstNumber = FirstNumber.Get(context);var secondNumber = SecondNumber.Get(context);var result =(int)Math.Pow(firstNumber + secondNumber,2);
ResultNumber.Set(context, result);}protected override void Execute(CodeActivityContext context)
{
var firstNumber = FirstNumber.Get(context);
var secondNumber = SecondNumber.Get(context);
var result = (int)Math.Pow(firstNumber + secondNumber, 2);
ResultNumber.Set(context, result);
}
Right-click the activity (in our case ActMathSquareOfSum), and from the Add menu, select Class.... The Add New Item window is displayed.
The Class item is already selected. All that is needed now is to rename it to DesignerMetadata.cs, and select Add. The new class is now added to the activity and opens up in a new tab.
Add the following content in the newly-created DesignerMetadata class:
using MathSquareOfSum;
using System.Activities.Presentation.Metadata;
using System.ComponentModel;
namespace ActMathSquareOfSum
{publicclassDesignerMetadata: IRegisterMetadata
{publicvoidRegister(){
AttributeTableBuilder attributeTableBuilder =newAttributeTableBuilder();
attributeTableBuilder.AddCustomAttributes(typeof(MathSqSum),newDesignerAttribute(typeof(MathSqSumDesigner)));
MetadataStore.AddAttributeTable(attributeTableBuilder.CreateTable());}}}using MathSquareOfSum;
using System.Activities.Presentation.Metadata;
using System.ComponentModel;
namespace ActMathSquareOfSum
{
public class DesignerMetadata : IRegisterMetadata
{
public void Register()
{
AttributeTableBuilder attributeTableBuilder = new AttributeTableBuilder();
attributeTableBuilder.AddCustomAttributes(typeof(MathSqSum), new DesignerAttribute(typeof(MathSqSumDesigner)));
MetadataStore.AddAttributeTable(attributeTableBuilder.CreateTable());
}
}
}