自定义Activity分两种,CodeActivity和NativeActivity。简单的区分就是CodeActivity只是执行一段代码,NativeActivity的效果就像内置Activities同样,它们实际上就是不一样Activity的父类,实现的时候选择继承哪一个类,你的Activity就是属于哪一个分类。git
咱们这里是实现CodeActivity,NativeActivity请看开源代码的实现。
功能是把特定分隔符链接的字符串分割开,而后随机返回其中的某一个。应用在给选择框一个随机的值。由于主要是学习的目的,因此实际上并无跟选择框有太大的关联,只是对字符作了处理而已。github
自定义Activity分两步,首先经过C#语言来编写你的Activity逻辑,编译生成.dll文件,而后经过NuGet Package Explorer打包。c#
下面跟着提示一步一步建立C#项目:mvc
System.Activities
和 System.ComponentModel.Composition
引用,并勾选。System.Activities
和 System.ComponentModel.Composition
这两个基础组件了。下面是已添加注释的实现代码:dom
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using System.Activities; using System.ComponentModel; namespace SelectRandomItem { public class SelectRandomItem : CodeActivity { //参数类型,输入或者输出,或者二者都是 [Category("Input")] //必须参数 [RequiredArgument] public InArgument<String> FullText { get; set; } [Category("Input")] //参数默认值 [DefaultValue("\r\n")] public InArgument<String> Separator { get; set; } [Category("Output")] public OutArgument<String> ChoiceResult { get; set; } /** * Execute是CodeActivity必须重载的方法 * 处理逻辑根据Separator指定的分割符分割FullText * 而后随机返回其中一个 * **/ protected override void Execute(CodeActivityContext context) { //全部的参数取值、赋值都是经过context var fullText = FullText.Get(context); var separator = Separator.Get(context); string[] items = Regex.Split(fullText, separator, RegexOptions.IgnoreCase); Random ran = new Random(); var result = items[ran.Next(items.Length)]; ChoiceResult.Set(context, result); } } }
而后点击 生成 > 生成 SelectRandomItem。在输出栏找到SelectRandomItem.dll文件所在位置,准备下一步打包使用。ide
Activities
”,否则UiPath会没法识别。ActivitiesSelectRandomItem.1.0.0.nupkg
。至此你的Activity就建立完成了。学习
该Activity的源文件都发布在我的github仓库,有须要请点击这里查看和下载。
同时该Activity在记事本自动录入项目中使用到两次,分别是随机选择字体和随机字体大小。对比我经过Python模块实现一样的功能来看,自定义Activity的执行速度比调用Python模块要稳定要快不少。字体
最后,谢谢你能看完!有不完善的地方还但愿与你们多交流。ui