SDK
最新
バナーの背景画像
開発者ガイド
最終更新日 2024年3月23日

アクティビティ コードを記述する

アクティビティ コードの記述方法を例示するため、GitHub からダウンロードできるサンプルの UiPath.Examples.Activities ソリューションに含まれる、単純な [計算機能] アクティビティを再作成します。このアクティビティは、2 つの数値と 1 つの演算 (加算、減算、乗算、または除算) を入力として受け取り、選択された演算の結果を返します。

アクティビティ コードは、アクティビティ ロジックとアクティビティ デザインの 2 つの部分で構成されます。

アクティビティ ロジック

UiPath.Activities.Template から始まり、ソリューションと、UiPath.Examples.Activities に関連するすべてのファイルと参照の名前を変更します。

  1. アクティビティ ロジックを保持するファイルの名前を ActivityTemplate.cs から Calculator.cs に変更します。
  2. 参照と名前空間を次のように更新します。

    using System.Activities;
    using System.Diagnostics;
    using UiPath.Examples.Activities.Helpers;
    
    namespace UiPath.Examples.Activities
    {
    }using System.Activities;
    using System.Diagnostics;
    using UiPath.Examples.Activities.Helpers;
    
    namespace UiPath.Examples.Activities
    {
    }
  3. 入力引数を宣言します。2 つの数値 FirstNumberSecondNumberint として、実行する操作 SelectedOperationenum として宣言し、オプションの既定値は Multiply に設定されています。 [RequiredArgument] 属性を使用して、3 つの引数すべてを必須としてマークします。戻り値は、Result 引数の値を設定するために使用されます。
    public class Calculator : CodeActivity<int> // This base class exposes an OutArgument named Result
        {
            [RequiredArgument]
            public InArgument<int> FirstNumber { get; set; } //InArgument allows a varriable to be set from the workflow
    
            [RequiredArgument]
            public InArgument<int> SecondNumber { get; set; }
    
            [RequiredArgument]
            public Operation SelectedOperation { get; set; } = Operation.Multiply; // default value is optional
    
            /*
             * The returned value will be used to set the value of the Result argument
             */
        }    public class Calculator : CodeActivity<int> // This base class exposes an OutArgument named Result
        {
            [RequiredArgument]
            public InArgument<int> FirstNumber { get; set; } //InArgument allows a varriable to be set from the workflow
    
            [RequiredArgument]
            public InArgument<int> SecondNumber { get; set; }
    
            [RequiredArgument]
            public Operation SelectedOperation { get; set; } = Operation.Multiply; // default value is optional
    
            /*
             * The returned value will be used to set the value of the Result argument
             */
        }
  4. 実行部分を開始して、ログ記録を追加し、ワークフロー コンテキストから数値の値を取得し、ゼロ除算のシナリオを処理するロジックを追加します。

    protected override int Execute(CodeActivityContext context)
            {
                // This is how you can log messages from your activity. logs are sent to the Robot which will forward them to Orchestrator
                context.GetExecutorRuntime().LogMessage(new Robot.Activities.Api.LogMessage()
                {
                    EventType = TraceEventType.Information,
                    Message = "Executing Calculator activity"
                });
    
                var firstNumber = FirstNumber.Get(context); //get the value from the workflow context (remember, this can be a variable)
                var secondNumber = SecondNumber.Get(context);
    
                if (secondNumber == 0 && SelectedOperation == Operation.Divide)
                {
                    throw new DivideByZeroException("Second number should not be zero when the selected operation is divide");
                }
    
                return ExecuteInternal(firstNumber, secondNumber);
            } protected override int Execute(CodeActivityContext context)
            {
                // This is how you can log messages from your activity. logs are sent to the Robot which will forward them to Orchestrator
                context.GetExecutorRuntime().LogMessage(new Robot.Activities.Api.LogMessage()
                {
                    EventType = TraceEventType.Information,
                    Message = "Executing Calculator activity"
                });
    
                var firstNumber = FirstNumber.Get(context); //get the value from the workflow context (remember, this can be a variable)
                var secondNumber = SecondNumber.Get(context);
    
                if (secondNumber == 0 && SelectedOperation == Operation.Divide)
                {
                    throw new DivideByZeroException("Second number should not be zero when the selected operation is divide");
                }
    
                return ExecuteInternal(firstNumber, secondNumber);
            }
  5. 選択した操作ごとに実行する計算を追加します。

    public int ExecuteInternal(int firstNumber, int secondNumber)
            {
                return SelectedOperation switch
                {
                    Operation.Add => firstNumber + secondNumber,
                    Operation.Subtract => firstNumber - secondNumber,
                    Operation.Multiply => firstNumber * secondNumber,
                    Operation.Divide => firstNumber / secondNumber,
                    _ => throw new NotSupportedException("Operation not supported"),
                };
            }        public int ExecuteInternal(int firstNumber, int secondNumber)
            {
                return SelectedOperation switch
                {
                    Operation.Add => firstNumber + secondNumber,
                    Operation.Subtract => firstNumber - secondNumber,
                    Operation.Multiply => firstNumber * secondNumber,
                    Operation.Divide => firstNumber / secondNumber,
                    _ => throw new NotSupportedException("Operation not supported"),
                };
            }
  6. 操作を定義します。

    public enum Operation
        {
            Add,
            Subtract,
            Multiply,
            Divide
        } public enum Operation
        {
            Add,
            Subtract,
            Multiply,
            Divide
        }

アクティビティの設計

アクティビティで使用できる入力は、プロパティのデータ型によって決まります。 サンプルの [計算機能] アクティビティでは、FirstNumber プロパティと SecondNumber プロパティは int データ型のため、プロパティの入力フィールドとして数値エディターが表示されます。一方、enum データ型である Operation では、アクティビティにドロップダウン メニューが使用できるようになります。

プロパティのラベルとツールチップは、Resources.resx ファイルで定義できます。

次の表で、各アクティビティ プロパティで使用できる最も一般的なプロパティについて説明します。

プロパティ説明
表示名プロパティのラベル。
ツールチッププロパティ上でホバーしたときに表示するテキスト。
IsRequired1プロパティが必須かどうか。 必須プロパティは、アクティビティ内で [RequiredArgument] 属性を使用してマークする必要もあります。
IsPrincipal2プロパティをアクティビティのメイン カテゴリに常に表示するかどうか。 [false] に設定すると、既定では折りたたまれた [詳細オプションを表示] メニューの下にプロパティが表示されます。
OrderIndexプロパティを表示する順序。

1 必須になることのない出力プロパティでは使用できません。

2 慣例により、出力プロパティは詳細オプションにおいてアクティビティの最後に配置されます。

[計算機能] アクティビティのデザインを作成する

  1. ファイル ActivityTemplateViewModel.cs の名前を CalculatorViewModel.cs に変更し、これに対するアクティビティのユーザー インターフェイスのコードを追加します。
  2. 参照と名前空間を次のように更新します。

    using System.Activities.DesignViewModels;
    using System.Diagnostics;
    
    namespace UiPath.Examples.Activities.ViewModels
    {
    }using System.Activities.DesignViewModels;
    using System.Diagnostics;
    
    namespace UiPath.Examples.Activities.ViewModels
    {
    }
  3. 入力プロパティを宣言します。 result プロパティは、アクティビティの基本クラスから取得されます。 名前と型引数は、アクティビティのものと一致する必要があります。

    public class CalculatorViewModel : DesignPropertiesViewModel
        {
            /*
             * Properties names must match the names and generic type arguments of the properties in the activity
             * Use DesignInArgument for properties that accept a variable
             */
            public DesignInArgument<int> FirstNumber { get; set; }
            public DesignInArgument<int> SecondNumber { get; set; }
            /*
             * Use DesignProperty for properties that accept a constant value                
             */
            public DesignProperty<Operation> SelectedOperation { get; set; }
             /*
             * The result property comes from the activity's base class
             */
            public DesignOutArgument<int> Result { get; set; }
            
            public CalculatorViewModel(IDesignServices services) : base(services)
            {
            }
        }public class CalculatorViewModel : DesignPropertiesViewModel
        {
            /*
             * Properties names must match the names and generic type arguments of the properties in the activity
             * Use DesignInArgument for properties that accept a variable
             */
            public DesignInArgument<int> FirstNumber { get; set; }
            public DesignInArgument<int> SecondNumber { get; set; }
            /*
             * Use DesignProperty for properties that accept a constant value                
             */
            public DesignProperty<Operation> SelectedOperation { get; set; }
             /*
             * The result property comes from the activity's base class
             */
            public DesignOutArgument<int> Result { get; set; }
            
            public CalculatorViewModel(IDesignServices services) : base(services)
            {
            }
        }
  4. アクティビティ デザインのコードを追加します。 必要に応じて、Debugger.Break(); を含む行のコメントを解除することで、ViewModel の初期化をデバッグするためのブレークポイントを追加できます。 ViewModel のプロパティを初期化し、初期化中にプロパティ値を変更するときに必須の PersistValuesChangedDuringInit() 呼び出しを追加し、アクティビティの入力プロパティと出力プロパティを定義します。

    コードは次のようになります。

    protected override void InitializeModel()
            {
               //Debugger.Break(); 
                /*
                 * The base call will initialize the properties of the view model with the values from the xaml or with the default values from the activity
                 */
                base.InitializeModel();
    
                PersistValuesChangedDuringInit(); // just for heads-up here; it's a mandatory call only when you change the values of properties during initialization
    
                var orderIndex = 0;
    
                FirstNumber.DisplayName = Resources.Calculator_FirstNumber_DisplayName;
                FirstNumber.Tooltip = Resources.Calculator_FirstNumber_Tooltip;
                /*
                 * Required fields will automatically raise validation errors when empty.
                 * Unless you do custom validation, required activity properties should be marked as such both in the view model and in the activity:
                 *   -> in the view model use the IsRequired property
                 *   -> in the activity use the [RequiredArgument] attribute.
                 */
                FirstNumber.IsRequired = true;
    
                FirstNumber.IsPrincipal = true; // specifies if it belongs to the main category (which cannot be collapsed)
                FirstNumber.OrderIndex = orderIndex++; // indicates the order in which the fields appear in the designer (i.e. the line number);
    
                SecondNumber.DisplayName = Resources.Calculator_SecondNumber_DisplayName;
                SecondNumber.Tooltip = Resources.Calculator_SecondNumber_Tooltip;
                SecondNumber.IsRequired = true;
                SecondNumber.IsPrincipal = true;
                SecondNumber.OrderIndex = orderIndex++;
    
                SelectedOperation.DisplayName = Resources.Calculator_SelectedOperation_DisplayName;
                SelectedOperation.Tooltip = Resources.Calculator_SelectedOperation_Tooltip;
                SelectedOperation.IsRequired = true;
                SelectedOperation.IsPrincipal = true;
                SelectedOperation.OrderIndex = orderIndex++;
    
                /*
                 * Output properties are never mandatory.
                 * By convention, they are not principal and they are placed at the end of the activity.
                 */
                Result.DisplayName = Resources.Calculator_Result_DisplayName;
                Result.Tooltip = Resources.Calculator_Result_Tooltip;
                Result.OrderIndex = orderIndex;
            } protected override void InitializeModel()
            {
               //Debugger.Break(); 
                /*
                 * The base call will initialize the properties of the view model with the values from the xaml or with the default values from the activity
                 */
                base.InitializeModel();
    
                PersistValuesChangedDuringInit(); // just for heads-up here; it's a mandatory call only when you change the values of properties during initialization
    
                var orderIndex = 0;
    
                FirstNumber.DisplayName = Resources.Calculator_FirstNumber_DisplayName;
                FirstNumber.Tooltip = Resources.Calculator_FirstNumber_Tooltip;
                /*
                 * Required fields will automatically raise validation errors when empty.
                 * Unless you do custom validation, required activity properties should be marked as such both in the view model and in the activity:
                 *   -> in the view model use the IsRequired property
                 *   -> in the activity use the [RequiredArgument] attribute.
                 */
                FirstNumber.IsRequired = true;
    
                FirstNumber.IsPrincipal = true; // specifies if it belongs to the main category (which cannot be collapsed)
                FirstNumber.OrderIndex = orderIndex++; // indicates the order in which the fields appear in the designer (i.e. the line number);
    
                SecondNumber.DisplayName = Resources.Calculator_SecondNumber_DisplayName;
                SecondNumber.Tooltip = Resources.Calculator_SecondNumber_Tooltip;
                SecondNumber.IsRequired = true;
                SecondNumber.IsPrincipal = true;
                SecondNumber.OrderIndex = orderIndex++;
    
                SelectedOperation.DisplayName = Resources.Calculator_SelectedOperation_DisplayName;
                SelectedOperation.Tooltip = Resources.Calculator_SelectedOperation_Tooltip;
                SelectedOperation.IsRequired = true;
                SelectedOperation.IsPrincipal = true;
                SelectedOperation.OrderIndex = orderIndex++;
    
                /*
                 * Output properties are never mandatory.
                 * By convention, they are not principal and they are placed at the end of the activity.
                 */
                Result.DisplayName = Resources.Calculator_Result_DisplayName;
                Result.Tooltip = Resources.Calculator_Result_Tooltip;
                Result.OrderIndex = orderIndex;
            }
  5. 次の図のように、ラベルとツールチップの文字列値を Resources.resx ファイルに追加します。 ローカリゼーションに向けて、アクティビティ名 (Activity name) とアクティビティの説明 (Activity description) には具体的なコメントを使用する必要があります。 その他の文字列については、コメントの追加をお勧めしますが、必須ではありません。


Studio では、設定の結果が次のようになります。



1 - 3 つの入力プロパティのラベル (表示名) です。

2 - FirstNumber プロパティのツールチップです。
  • アクティビティ ロジック
  • アクティビティの設計
  • [計算機能] アクティビティのデザインを作成する

Was this page helpful?

サポートを受ける
RPA について学ぶ - オートメーション コース
UiPath コミュニティ フォーラム
UiPath ロゴ (白)
信頼とセキュリティ
© 2005-2024 UiPath. All rights reserved.