SDK
最新
False
横幅背景图像
开发者指南
上次更新日期 2024年3月23日

如何创建自定义触发器

自动化项目受益于触发器,该触发器监控计算机活动中的特定事件,以便触发特定 Actions。 可以通过“ 监控事件”框架配置触发器,但您也可以构建自定义触发器,如本指南中所述。

先决条件

要创建自定义触发器,需要满足以下条件:

  • Microsoft Visual Studio
  • Microsoft .NET Framework v4.6.1 或更高版本

创建和配置项目

  1. 打开 Microsoft Visual Studio 并选择“新建项目”。 系统将显示“项目选择”窗口。
  2. 选择 “类库 (.NET Framework)”,然后单击 “下一步”。 系统将显示“ 配置新项目 ”窗口。
  3. 提供项目名称、位置、解决方案名称和框架。 确保选择 .NET Framework 4.6.1 或更高版本。 填写所有字段后,单击“ 创建”。 系统将创建新项目,并显示设计器窗口。



  4. 从“ 工具 ”菜单中,选择“ 选项”。 现在将显示“ 选项 ” 窗口。
  5. 展开“ NuGet 包管理器 ”条目,然后选择“ 包来源”。
  6. 添加新的包来源,并用 UiPath 官方订阅 填充“名称” 字段,并用 https://pkgs.dev.azure.com/uipath/Public.Feeds/_packaging/UiPath-Official/nuget/v3/index.json填充“来源” 字段。 单击“ 确定 ”以确认并保存更改。


  7. 单击“ 工具 ” 菜单,然后从“ NuGet 包管理器 ” 条目中选择 “管理解决方案的 NuGet 包…”。 系统将显示“ NuGet - 解决方案 ”选项卡。
  8. 搜索 UiPath.Platform 引用并将其选中。 在右侧面板中,选择要为其添加引用的项目,然后单击“ 安装”。 确保选中“ 包括预发行版 ” 复选框,并安装 UiPath.Platform v20.8 参考或更高版本。

编写触发器代码

Once the references are added to the project, it's time to write the code, which should look something like this:

using System; 
using System.Activities; 
using System.Threading; 
using UiPath.Platform.Triggers; 
 
public class TimerTrigger : TriggerBase<TimerTriggerArgs> 
{ 
 //it is recommended to use Variable to store fields in order for  
 //activities like Parallel For Each to work correctly 
   private readonly Variable<Timer> _timer = new Variable<Timer>();   
  public InArgument<TimeSpan> Period { get; set; }    
protected override void CacheMetadata(NativeActivityMetadata metadata) 
    { 
        metadata.AddImplementationVariable(_timer); 
        base.CacheMetadata(metadata); 
    }     
  
//in this method you can subscribe to events. It is called when the trigger starts execution 
protected override void StartMonitor(NativeActivityContext context, Action<TimerTriggerArgs> sendTrigger) 
    { 
        var eventIndex = 0; 
        var period = Period.Get(context); 
        _timer.Set(context, new Timer(OnTick, state: null, dueTime: period, period: period)); 
        return;  
void OnTick(object state) => sendTrigger(new TimerTriggerArgs(eventIndex++));    
}     
 
   //this is used for cleanup. It is called when the trigger is Cancelled or Aborted 
   protected override void StopMonitor(ActivityContext context) => _timer.Get(context).Dispose(); 
} 
 
//Each trigger may declare a type that sub-classes TriggerArgs 
//that corresponds to the “args” item in Trigger Scope activity. If no extra info 
//needs to be passed along, TriggerArgs can be used directly 
public class TimerTriggerArgs : TriggerArgs 
{ 
    public int EventIndex { get; } 
 
    public TimerTriggerArgs(int eventIndex) => EventIndex = eventIndex; 
}using System; 
using System.Activities; 
using System.Threading; 
using UiPath.Platform.Triggers; 
 
public class TimerTrigger : TriggerBase<TimerTriggerArgs> 
{ 
 //it is recommended to use Variable to store fields in order for  
 //activities like Parallel For Each to work correctly 
   private readonly Variable<Timer> _timer = new Variable<Timer>();   
  public InArgument<TimeSpan> Period { get; set; }    
protected override void CacheMetadata(NativeActivityMetadata metadata) 
    { 
        metadata.AddImplementationVariable(_timer); 
        base.CacheMetadata(metadata); 
    }     
  
//in this method you can subscribe to events. It is called when the trigger starts execution 
protected override void StartMonitor(NativeActivityContext context, Action<TimerTriggerArgs> sendTrigger) 
    { 
        var eventIndex = 0; 
        var period = Period.Get(context); 
        _timer.Set(context, new Timer(OnTick, state: null, dueTime: period, period: period)); 
        return;  
void OnTick(object state) => sendTrigger(new TimerTriggerArgs(eventIndex++));    
}     
 
   //this is used for cleanup. It is called when the trigger is Cancelled or Aborted 
   protected override void StopMonitor(ActivityContext context) => _timer.Get(context).Dispose(); 
} 
 
//Each trigger may declare a type that sub-classes TriggerArgs 
//that corresponds to the “args” item in Trigger Scope activity. If no extra info 
//needs to be passed along, TriggerArgs can be used directly 
public class TimerTriggerArgs : TriggerArgs 
{ 
    public int EventIndex { get; } 
 
    public TimerTriggerArgs(int eventIndex) => EventIndex = eventIndex; 
}
下一步是实现 StartMonitorStopMonitor 方法。 这样做是为了规定触发器的行为,以监控特定事件。
需要注意的是,如果要提供参数以在事件处理程序中扩展触发器的用法,则需要在设计器中添加并配置 <Custom>TriggerArgs 类。

最后一步是 构建库创建要在 Studio 中使用的 NuGet 包 。 现在可以在“ 触发器作用域 ” 活动中使用新触发器。

  • 先决条件
  • 创建和配置项目
  • 编写触发器代码

此页面是否有帮助?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath 白色徽标
信任与安全
© 2005-2024 UiPath. All rights reserved.