本文轉自:https://segmentfault.com/a/1190000017440647
為了對UiPath Activity的實現方式一探究竟,自己嘗試實踐編寫了一個簡單的Activity,取名叫SelectRandomItem。
1. 開發環境准備:
- Microsoft Visual Studio with the .NET C# desktop development workload installed.
- NuGet Package Explorer.
- UiPath
2. Activity概述
自定義Activity分兩種,CodeActivity和NativeActivity。簡單的區分就是CodeActivity只是執行一段代碼,NativeActivity的效果就像內置Activities一樣,它們實際上就是不同Activity的父類,實現的時候選擇繼承哪個類,你的Activity就是屬於哪個分類。
我們這里是實現CodeActivity,NativeActivity請看開源代碼的實現。
功能是把特定分隔符連接的字符串分割開,然后隨機返回其中的某一個。應用在給選擇框一個隨機的值。因為主要是學習的目的,所以實際上並沒有跟選擇框有太大的關聯,只是對字符做了處理而已。
自定義Activity分兩步,首先通過C#語言來編寫你的Activity邏輯,編譯生成.dll文件,然后通過NuGet Package Explorer打包。
3.創建C#項目,編寫實現代碼
下面跟着提示一步一步創建C#項目:
- Launch Microsoft Visual Studio.
- Click 文件 > 創建 > 項目 (shortcut: Ctrl + Shift + N). The New Project window is displayed.
- Click Visual C#. The list of all dependencies using C# is displayed.
- 給你的Activity取個名字, 這里是 “SelectRandomItem”。
- 選擇類庫(.NET Framework) and click OK. 這樣才能把項目導出為 .dll文件。
- Click 項目 > 添加引用….
- 分別搜索
System.Activities
和System.ComponentModel.Composition
引用,並勾選。 - Click the OK button.這樣就可以在代碼中使用
System.Activities
和System.ComponentModel.Composition
這兩個基礎組件了。
下面是已添加注釋的實現代碼:
-
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
-
{
-
//參數類型,輸入或者輸出,或者兩者都是
-
[
-
//必須參數
-
[
-
public InArgument<String> FullText { get; set; }
-
-
[
-
//參數默認值
-
[
-
public InArgument<String> Separator { get; set; }
-
-
[
-
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文件所在位置,准備下一步打包使用。
4. 使用NuGet Package Explorer打包
- 打開NuGet Package Explorer。點擊Create a new package (Ctrl + N),你會看到左右分割的兩欄Package metadata和Package contents。
- 在右邊Package contents欄的右鍵菜單單擊 Add lib folder。
- 在lib文件夾上右鍵,點擊 Add Existing File… 添加 SelectRandomItem.dll 文件。
- 點擊頂部菜單欄 Edit > Edit Metadata,填寫你的包信息。需要注意的是Id字段必須包含 “
Activities
”,不然UiPath會無法識別。 - 點擊 File > Save ,保存你的包,文件名應該是類似這樣:
ActivitiesSelectRandomItem.1.0.0.nupkg
。
至此你的Activity就創建完成了。
添加到UiPath和你的項目當中實際使用
- 首先確定你的UiPath本地包的目錄,打開Manage Packages,點擊左上方的Settings,在Default packages sources欄中查看Local標簽對應的路徑。把你創建的包放到這個文件夾內。
- 在Manage Packages左側點擊All Packages > Local ,你或者應該可以直接看到你的包了或者通過搜索來找到你的包。
- 點擊你的包,在右側信息欄點擊install,然后Save,至此你的包已經安裝到你的UiPath Studio的Activities面板中了。
- 拖到你項目中體驗一下吧:)
5. 代碼文件下載
該Activity的源文件都發布在個人github倉庫,有需要請點擊這里查看和下載。
同時該Activity在記事本自動錄入項目中使用到兩次,分別是隨機選擇字體和隨機字體大小。對比我通過Python模塊實現同樣的功能來看,自定義Activity的執行速度比調用Python模塊要穩定要快很多。
最后,謝謝你能看完!有不完善的地方還希望與大家多交流。