订阅

UiPath 开发者

The UiPath Developer Guide

构建活动项目设置

Using the UiPath.Activities.API package from the Official feed (https://pkgs.dev.azure.com/uipath/Public.Feeds/_packaging/UiPath-Official/nuget/v3/index.json), you can create and add your own custom activity project settings in Studio. For information on how to use the API, see About the Activities SDK.

📘

重要

UiPath.Activities.API 包必须用作自定义项目中的开发依赖项。阅读有关开发依赖性的更多信息。

Activity project settings encompass a set of options that can be adjusted on a project level directly from the Project Settings window.

1082

构建活动设置

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

添加选项卡和部分

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);

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

1047

添加设置

  • 可展开的设置部分:
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;
        }

结果为:

938
  • 布尔值单选按钮:
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);
        }

结果为:

785
  • 值的多项选择列表:
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);
        }

结果为:

783
  • 输入需要验证的文本字段:
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 `{s}`";
private static string ValidateSimpleStringSetting(string arg)
        {
            if (arg?.ToLowerInvariant().Contains("invalid") == true)
            {
                return "The sample string setting is invalid if it contains the `invalid` keyword";
            }

            return string.Empty;
        }

结果为:

781

获取流程 ID 和日志

通过使用适用于引用 IExecutorRuntime 的方法,您可以获得有关机器人正在执行的流程 ID 的信息,以及执行日志。可以使用以下方法:

  • IRunningJobInformation - 收集正在执行的进程的相关信息,并支持以下属性:
    • JobID - 检索流程 ID
    • ProcessName - 检索流程名称
    • ProcessVersion - 检索流程版本
    • WorkflowFilePath - 检索流程的完整路径
    • InitiatedBy - 检索作业的来源(机器人、Studio、Orchestrator 等)
    • FolderId - retrieves the folder id of the process
    • FolderName - retrieves the folder name of the process
    • TenantId - retrieves the tenant id
    • TenantKey - retrieves the tenant key
    • TenantName - retrieves the tenant name
    • RobotName - retrieves the robot name
    • LicenseType - retrieves the robot license type
    • RuntimeGovernanceEnabled - provides info if runtime governance is enabled or not
    • InternalArguments - retrieves any internal arguments of the process
    • OrganizationId - retrieves the organization id
    • PictureInPictureMode - retrieves the type of Picture in Picture mode used. The following values are available:
      • Main - the process runs in the main Windows session
      • PictureInPictureSession - the process runs in the PiP session
      • PictureInPictureDesktop - the process runs in PiP virtual desktop mode
  • LogMessage - Generates Robot Execution Logs according to a Logging Level you specify. Read more about logs on this page. The following properties are supported:
    • 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}"
        });
    }
}

将项目添加到 Studio

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

创建 NuGet 包

1280
  1. 启动 NuGet Package Explorer
  2. 单击“创建新包 (Ctrl + N)”。随即会出现一个分割窗口,其中显示“包元数据”和“包内容”。我们需要在窗口的后一部分中添加所有依赖项。
  3. 右键单击“包内容”部分。系统随即会显示一个上下文菜单。
  4. 单击“添加库文件夹”。请注意,系统会在“包内容”部分创建新的“库”项目。
  5. 右键单击“库”并选择“添加现有文件…”
  6. 加载项目的外部程序集 (.dll)。
  7. 单击“编辑”>“编辑元数据”。随即会显示“包元数据”部分。
  8. 根据需要填写字段,以更好地描述项目。
  9. 填写“ID”字段,并确保包含关键字“活动”,以便在 Studio 的“管理包”窗口中显示该包。
  10. 单击“文件”>“保存”。本示例中,我们已创建“.nupkg”文件。

📘

备注:

务必为活动创建直观的文件夹结构。与 Orchestrator 一起使用时,系统会删除自定义活动中的所有空文件夹。

在 UiPath Studio 中安装 NuGet 包

Once the .nupkg file is created, add it to a custom feed in Studio, as explained here.

Open the Manage Packages window and install the package. Make sure the custom feed is enabled in Studio.

示例设置

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

1049

Simply follow the link below to download the sample, which also contains an example of creating wizards.

下载示例

3个月前更新


构建活动项目设置


建议的编辑仅限用于 API 参考页面

您只能建议对 Markdown 正文内容进行编辑,而不能建议对 API 规范进行编辑。