UiPath Documentation
sdk
latest
false
开发者指南
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。

如何创建自定义触发器

使用监控事件框架创建自定义触发器,以监控计算机活动并根据特定事件触发操作。

自动化项目受益于触发器,该触发器监控计算机活动中的特定事件,以便触发特定 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.uipath.com/official/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; 
}

下一步是实现 StartMonitor 和 StopMonitor 方法。 这样做是为了规定触发器的行为,以监控特定事件。

需要注意的是,如果要提供参数以在事件处理程序中扩展触发器的用法,则需要在设计器中添加并配置 <Custom>TriggerArgs 类。

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

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

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新