- 概述
- 自定义活动
- 将活动迁移到 .NET 6
- 发行说明
- 构建工作流分析器规则
- 构建活动项目设置
- 创建自定义向导
- 按范围划分活动的优先级
- UiPath.Activities.Api.Base
- UiPath.Studio.Activities.Api
- UiPath.Studio.Activities.Api.Activities
- UiPath.Studio.Activities.Api.BusyService
- UiPath.Studio.Activities.Api.ExpressionEditor
- UiPath.Studio.Activities.Api.Expressions
- UiPath.Studio.Activities.Api.Licensing
- UiPath.Studio.Activities.Api.Mocking
- UiPath.Studio.Activities.Api.ObjectLibrary
- UiPath.Studio.Activities.Api.PackageBindings
- UiPath.Studio.Activities.Api.ProjectProperties
- UiPath.Studio.Activities.Api.ScopedActivities
- UiPath.Studio.Activities.Api.Settings
- UiPath.Studio.Activities.Api.Wizards
- UiPath.Studio.Activities.Api.Workflow
- UiPath.Studio.Api.Controls
- UiPath.Studio.Api.Telemetry
- UiPath.Studio.Api.Theme
- Robot JavaScript SDK
- 触发器 SDK
- 如何创建自定义触发器
- 智能体 SDK

开发者指南
如何创建自定义触发器
自动化项目受益于触发器,该触发器监控计算机活动中的特定事件,以便触发特定 Actions。 可以通过“ 监控事件”框架配置触发器,但您也可以构建自定义触发器,如本指南中所述。
先决条件
要创建自定义触发器,需要满足以下条件:
- Microsoft Visual Studio
- Microsoft .NET Framework v4.6.1 或更高版本
创建和配置项目
-
打开 Microsoft Visual Studio 并选择“新建项目”。 系统将显示“项目选择”窗口。
-
选择“类库 (.NET Framework)” ,然后单击“下一步” 。系统将显示“配置新项目”窗口。
-
提供项目名称、位置、解决方案名称和框架。确保选择“.NET Framework 4.6.1或更高版本。填写所有字段后,单击“创建” 。系统将创建新项目,并显示设计器窗口。

-
从“工具”菜单中,选择“选项” 。现在会显示“选项”窗口。
-
展开“NuGet 包管理器”条目,然后选择“包来源” 。
-
添加新的包来源,并在“名称”字段中填充 UiPath Official Feed,在“来源”字段中填充
https://pkgs.dev.azure.com/uipath/Public.Feeds/_packaging/UiPath-Official/nuget/v3/index.json。单击“确定”以确认并保存更改。
-
单击工具菜单,然后从NuGet 包管理器条目中选择“管理解决方案的 NuGet 包…” 。系统将显示“NuGet - 解决方案”选项卡。
-
搜索“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;
}
下一步是实现 StartMonitor 和 StopMonitor 方法。 这样做是为了规定触发器的行为,以监控特定事件。
需要注意的是,如果要提供参数以在事件处理程序中扩展触发器的用法,则需要在设计器中添加并配置 <Custom>TriggerArgs 类。
最后一步是构建库并创建要在 Studio 中使用的 NuGet 包。现在可以在“触发器作用域”活动中使用新的触发器。