sdk
latest
false
重要 :
请注意此内容已使用机器翻译进行了部分本地化。
UiPath logo, featuring letters U and I in white
开发者指南
Last updated 2024年10月25日

构建活动项目设置

使用 官方 订阅源 ( ) 中的 UiPath.Activities.APIhttps://pkgs.dev.azure.com/uipath/Public.Feeds/_packaging/UiPath-Official/nuget/v3/index.json 包,您可以在 Studio 中创建和添加自己的自定义活动项目设置。有关如何使用 API 的信息,请参阅Studio 活动 SDK
注意: UiPath Activities.API 包必须用作自定义项目中的开发依赖项。 阅读有关 开发依赖项的更多信息。

活动项目设置包含一组选项,您可以直接从“ 项目设置” 窗口在项目级别调整这些选项。



构建活动设置

下方提供为音乐播放器示例活动构建一组设置的示例。

添加选项卡和部分

using UiPath.Studio.Activities.Api;
using UiPath.Studio.Activities.Api.Settings;
namespace MyCustomActivityPack
{
    public static class SettingsCreator
    {
        // This is the key used to reference a tab, even across packages.
        internal const string CategoryKey = "DemoTabUniqueKey";
        // This is the key used to reference the setting, even across packages.
        internal const string CommentTextKey = CategoryKey + ".Comment";
        internal const string PresetKey = CategoryKey + ".Preset";
        internal const string ShuffleKey = CategoryKey + ".Shuffle";
        public static void CreateSettings(IWorkflowDesignApi workflowDesignApi)
        {
            var settingsApi = workflowDesignApi.Settings;
            // Add the category (a tab in the settings page)
            var category = new SettingsCategory()
            {
                Description = "Settings Sample",
                Header = "Settings Sample",
                Key = CategoryKey
            };
            settingsApi.AddCategory(category);
            AddMusicPlayerSection(settingsApi, category);
            AddMiscSettings(settingsApi, category);using UiPath.Studio.Activities.Api;
using UiPath.Studio.Activities.Api.Settings;
namespace MyCustomActivityPack
{
    public static class SettingsCreator
    {
        // This is the key used to reference a tab, even across packages.
        internal const string CategoryKey = "DemoTabUniqueKey";
        // This is the key used to reference the setting, even across packages.
        internal const string CommentTextKey = CategoryKey + ".Comment";
        internal const string PresetKey = CategoryKey + ".Preset";
        internal const string ShuffleKey = CategoryKey + ".Shuffle";
        public static void CreateSettings(IWorkflowDesignApi workflowDesignApi)
        {
            var settingsApi = workflowDesignApi.Settings;
            // Add the category (a tab in the settings page)
            var category = new SettingsCategory()
            {
                Description = "Settings Sample",
                Header = "Settings Sample",
                Key = CategoryKey
            };
            settingsApi.AddCategory(category);
            AddMusicPlayerSection(settingsApi, category);
            AddMiscSettings(settingsApi, category);

这将向“活动项目设置”窗口添加一个选项卡和一个部分。将鼠标悬停在选项卡的工具提示上时,选项卡说明即会显示:



添加设置

  • 可展开的设置部分:

    private static SettingsSection AddMusicPlayerSection(IActivitiesSettingsService settingsApi, SettingsCategory category)
            {
                var section = new SettingsSection()
                {
                    Description = "Settings for a music player",
                    IsExpanded = true, //set this to control default expansion
                    Title = "Music Player Settings",
                    // the key of a section has to be unique only within the category
                    Key = "Player"
                };
                settingsApi.AddSection(category, section);
                AddSimpleBoolean(settingsApi, section);
                AddSingleChoice(settingsApi, section);
                return section;
            }private static SettingsSection AddMusicPlayerSection(IActivitiesSettingsService settingsApi, SettingsCategory category)
            {
                var section = new SettingsSection()
                {
                    Description = "Settings for a music player",
                    IsExpanded = true, //set this to control default expansion
                    Title = "Music Player Settings",
                    // the key of a section has to be unique only within the category
                    Key = "Player"
                };
                settingsApi.AddSection(category, section);
                AddSimpleBoolean(settingsApi, section);
                AddSingleChoice(settingsApi, section);
                return section;
            }

结果为:



  • 布尔值单选按钮:

    private static void AddSimpleBoolean(IActivitiesSettingsService settingsApi, SettingsSection section)
            {
                var booleanSetting = new SingleValueEditorDescription<bool>
                {
                    DefaultValue = true,
                    Description = "If active, the playlist is shuffled",
                    // The value returned by GetDisplayValue should be localized
                    GetDisplayValue = b => b ? "On" : "Off",
                    Key = ShuffleKey,
                    Label = "Shuffle"
                };
                settingsApi.AddSetting(section, booleanSetting);
            }private static void AddSimpleBoolean(IActivitiesSettingsService settingsApi, SettingsSection section)
            {
                var booleanSetting = new SingleValueEditorDescription<bool>
                {
                    DefaultValue = true,
                    Description = "If active, the playlist is shuffled",
                    // The value returned by GetDisplayValue should be localized
                    GetDisplayValue = b => b ? "On" : "Off",
                    Key = ShuffleKey,
                    Label = "Shuffle"
                };
                settingsApi.AddSetting(section, booleanSetting);
            }

结果为:



  • 值的多项选择列表:

    private static void AddSingleChoice(IActivitiesSettingsService settingsApi, SettingsSection category)
            {
                var booleanSetting = new SingleValueSelectorDescription
                {
                    DefaultValue = "pop",
                    Values = new[] { "classic", "pop", "rock" },
                    Description = "Sample single choice setting",
                    // The value returned by GetDisplayValue should be localized
                    GetDisplayValue = choice => choice + " music",
                    Key = PresetKey,
                    Label = "Mixer Preset"
                };
                settingsApi.AddSetting(category, booleanSetting);
            }private static void AddSingleChoice(IActivitiesSettingsService settingsApi, SettingsSection category)
            {
                var booleanSetting = new SingleValueSelectorDescription
                {
                    DefaultValue = "pop",
                    Values = new[] { "classic", "pop", "rock" },
                    Description = "Sample single choice setting",
                    // The value returned by GetDisplayValue should be localized
                    GetDisplayValue = choice => choice + " music",
                    Key = PresetKey,
                    Label = "Mixer Preset"
                };
                settingsApi.AddSetting(category, booleanSetting);
            }

结果为:



  • 输入需要验证的文本字段:

    private static void AddSimpleString(IActivitiesSettingsService settingsApi, SettingsSection section)
            {
                var simpleStringSetting = new SingleValueEditorDescription<string>()
                {
                    Description = "A free text comment that can't contain the word 'invalid'",
                    // The GetDisplayValue obtains a localized screen representation of the underlying setting value.
                    GetDisplayValue = LocalizeSimpleSettingValue,
                    IsReadOnly = false,
                    DefaultValue = "There is no comment",
                    Label = "Comment",
                    Validate = ValidateSimpleStringSetting,
                    Key = CommentTextKey
                };
                // Add the setting to the section.
                // A setting may also be directly added on a category. It will appear as a setting without a section (top level setting)
                settingsApi.AddSetting(section, simpleStringSetting);
            }
    private static string LocalizeSimpleSettingValue(string s) => $"A localized value of <code>{s}</code>";
    private static string ValidateSimpleStringSetting(string arg)
            {
                if (arg?.ToLowerInvariant().Contains("invalid") == true)
                {
                    return "The sample string setting is invalid if it contains the <code>invalid</code> keyword";
                }
                return string.Empty;
            }private static void AddSimpleString(IActivitiesSettingsService settingsApi, SettingsSection section)
            {
                var simpleStringSetting = new SingleValueEditorDescription<string>()
                {
                    Description = "A free text comment that can't contain the word 'invalid'",
                    // The GetDisplayValue obtains a localized screen representation of the underlying setting value.
                    GetDisplayValue = LocalizeSimpleSettingValue,
                    IsReadOnly = false,
                    DefaultValue = "There is no comment",
                    Label = "Comment",
                    Validate = ValidateSimpleStringSetting,
                    Key = CommentTextKey
                };
                // Add the setting to the section.
                // A setting may also be directly added on a category. It will appear as a setting without a section (top level setting)
                settingsApi.AddSetting(section, simpleStringSetting);
            }
    private static string LocalizeSimpleSettingValue(string s) => $"A localized value of <code>{s}</code>";
    private static string ValidateSimpleStringSetting(string arg)
            {
                if (arg?.ToLowerInvariant().Contains("invalid") == true)
                {
                    return "The sample string setting is invalid if it contains the <code>invalid</code> keyword";
                }
                return string.Empty;
            }

结果为:



获取流程 ID 和日志

通过使用适用于引用 IExecutorRuntime 的方法,您可以获得有关机器人正在执行的流程 ID 的信息,以及执行日志。可以使用以下方法:
  • IRunningJobInformation - 收集正在执行的进程的相关信息,并支持以下属性:
    • JobID - 检索流程 ID
    • ProcessName - 检索流程名称
    • ProcessVersion - 检索流程版本
    • WorkflowFilePath - 检索流程的完整路径
    • InitiatedBy - 检索作业的来源(机器人、Studio、Orchestrator 等)
    • FolderId - 检索流程的文件夹 ID
    • FolderName - 检索流程的文件夹名称
    • TenantId - 检索租户 ID
    • TenantKey - 检索租户密钥
    • TenantName - 检索租户名称
    • RobotName - 检索机器人名称
    • LicenseType - 检索机器人许可证类型
    • RuntimeGovernanceEnabled - 提供是否启用 Runtime 监管的信息
    • InternalArguments - 检索流程的任何内部参数
    • OrganizationId - 检索组织 ID
    • PictureInPictureMode - 检索所用画中画模式的类型。 以下值可用:
      • Main - 流程在主 Windows 会话中运行
      • PictureInPictureSession - 流程在 PiP 会话中运行
      • PictureInPictureDesktop - 流程在 PiP 虚拟桌面模式下运行
    • LogMessage - 根据您指定的 日志记录级别 生成 机器人执行日志 。 在此页面上阅读有关日志的更多信息。 支持以下属性:
    • EventType - 要检索的日志记录级别
    • Message - 要显示的消息
以下代码示例可说明如何使用 LogMessage 方法:
public class MyLogMessageActivity : CodeActivity
{
    protected override void Execute(CodeActivityContext context)
    {
        var executorRuntime = context.GetExtension<IExecutorRuntime>();
        var jobInfo = executorRuntime.RunningJobInformation;
        executorRuntime.LogMessage(new LogMessage
        {
            EventType = TraceEventType.Warning,
            Message = $"Job {jobInfo.JobId}: My log message from workflow {jobInfo.WorkflowFilePath}"
        });
    }
}public class MyLogMessageActivity : CodeActivity
{
    protected override void Execute(CodeActivityContext context)
    {
        var executorRuntime = context.GetExtension<IExecutorRuntime>();
        var jobInfo = executorRuntime.RunningJobInformation;
        executorRuntime.LogMessage(new LogMessage
        {
            EventType = TraceEventType.Warning,
            Message = $"Job {jobInfo.JobId}: My log message from workflow {jobInfo.WorkflowFilePath}"
        });
    }
}

将项目添加到 Studio

要使设置在 Studio 的“活动项目设置'”窗口可见,您需要将自定义活动发布到 NuGet 包中,并向 2019.10.1 或更高版本 Studio 中定义的订阅源提供该包。

创建 NuGet 包



  1. Launch NuGet Package Explorer.
  2. 单击“创建新包 (Ctrl + N)”。随即会出现一个分割窗口,其中显示“包元数据”和“包内容”。我们需要在窗口的后一部分中添加所有依赖项。
  3. 右键单击“包内容”部分。系统随即会显示一个上下文菜单。
  4. 单击 “添加库文件夹” 请注意,系统会在“ 包内容 ”部分中创建一个新的库项目。
  5. 右键单击“ ”,然后选择“ 添加现有文件…”
  6. 加载项目的外部程序集 (.dll)。
  7. 单击 “编辑” >“编辑元数据”。 系统将显示“ 包元数据 ” 部分。
  8. 根据需要填写字段,以更好地描述项目。
  9. 填写“ID”字段,并确保包含关键字“活动”,以便在 Studio 的“管理包”窗口中显示该包。
  10. Click File > Save. In our case, the .nupkg file is created.
    Note: Be sure to create an intuitive folder structure for your activities. All empty folders inside the custom activity get removed when used with Orchestrator.

在 UiPath Studio 中安装 NuGet 包

创建 .nupkg 文件后,将其添加到 Studio 中的自定义订阅源,如此 所述。

打开“ 管理包 ”窗口并安装包。 确保在 Studio 中启用了自定义订阅源。

示例设置

集中放置上方创建的选项卡、部分和设置,以使其在“活动项目设置”窗口中可见:



只需点击下面的链接即可下载示例,其中还包含“ 创建自定义向导”的示例。

此页面有帮助吗?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath Logo White
信任与安全
© 2005-2024 UiPath。保留所有权利。