sdk
latest
false
重要 :
このコンテンツの一部は機械翻訳によって処理されており、完全な翻訳を保証するものではありません。 新しいコンテンツの翻訳は、およそ 1 ~ 2 週間で公開されます。
UiPath logo, featuring letters U and I in white

開発者ガイド

最終更新日時 2025年10月30日

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

この例では、アクティビティ コードの記述を示すために、UiPath.Examples.Activities GitHub の ソリューションから単純な [ 計算機能] アクティビティを再作成します。このアクティビティは、以下のとおりです。
  • 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. アクティビティ デザインのコードを追加します。必要に応じて、 ViewModel 初期化をデバッグするには、 Debugger.Break();を含む行のコメントを解除してブレークポイントを追加します。
    次のコード スニペットは、 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) には具体的なコメントを使用する必要があります。その他の文字列については、コメントを追加することをお勧めします。


次の図は、UiPath Studio で表示されたアクティビティの設定を示しています。



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

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

このページは役に立ちましたか?

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