自定義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