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

開発者ガイド

最終更新日時 2026年3月30日

カスタム アクティビティのコードを記述する

カスタム アクティビティのコードの書き方をよりよく理解するために、ユーザーに 2 つの数字を求め、合計の四角形を出力する単純なアクティビティを作成してみましょう。

  1. Microsoft Visual Studio を起動します。

  2. [ ファイル] > [新規>プロジェクト... ] を選択します (ショートカット: Ctrl + Shift + N)。[新しいプロジェクト] ウィンドウが表示されます。

  3. [言語] ドロップダウン メニューから [C#] を選択します。C# を使用するすべての依存関係のリストが表示されます。

  4. [クラス ライブラリ (.NET Framework)] を選択します。これは、カスタム アクティビティを .dll ファイルとしてエクスポートするのに役立ちます。[次へ] を選択して [新しいプロジェクトを設定] ウィンドウに移動します。

    Microsoft Visual Studio の [Create a new project] ウィンドウ

  5. [ プロジェクト名 ] フィールドに目的のアクティビティ名を入力します。この場合、「MathSquareOfSum」を使用できます。

  6. [フレームワーク] ドロップダウン メニューから [.NET Framework 4.6.1] を選択します。これにより、ライブラリと UiPath Studio の相互運用性が確保されます。

    重要:

    Windows - レガシ プロジェクトでは、UiPath Studio は .NET Framework 4.5.2 から 4.6.1 で作成されたアクティビティをサポートします。Windows 対応のプロジェクトで使用するためにアクティビティを .NET に移行する方法については、「 アクティビティを .NET 6 に移行する」をご覧ください。

    新しいプロジェクトを設定する

  7. [ 作成 ] を選択してコード デザイナーに移動し、アクティビティ コードの記述を開始します。

  8. [ ソリューション エクスプローラー ] パネルで [ 参照 ] ブランチを右クリックし、[ 参照を追加...] を選択します。[参照マネージャー] ウィンドウが表示されます。

  9. System.Activitiesを検索し、参照System.ComponentModel.Compositionして追加します。それぞれの前にあるチェックボックスをオンにして、[ OK]をクリックしてください。これにより、前述の参照のクラスを使用できるようになります。

    System.Activities アセンブリが選択されている [参照マネージャー] ウィンドウ

    System.ComponentModel.Composition アセンブリが選択された [参照マネージャー] ウィンドウ

  10. 現在は、新たに追加された参照がコードで使用されるようにする必要があります。 それには、コード デザイナーに以下の行を追加します。

    using System.Activities;
    using System.ComponentModel;
    using System.Activities;
    using System.ComponentModel;
    

    プロジェクトは次のようになります。

    前のコードブロックを追加した後のプロジェクトの外観

  11. 入力および出力パラメーターを追加します。 この例では、コードは次のようになります。

    //Note that these attributes are localized so you need to localize this attribute for Studio languages other than English
     
    //Dots allow for hierarchy. App Integration.Excel is where Excel activities are.
    [Category("Category.Where.Your.Activity.Appears.In.Toolbox")]
    [DisplayName("Human readable name instead of class name")]
    [Description("The text of the tooltip")]
     public class MathSqSum : CodeActivity
    {
     
        //Note that these attributes are localized so you need to localize this attribute for Studio languages other than English
                 [Category("Input")]
            [DisplayName("First Number")]
            [Description("Enter the first number")]
            [RequiredArgument]
            public InArgument<int> FirstNumber { get; set; }
            [Category("Input")]
            [DisplayName("Second Number")]
            [Description("Enter the second number")]
            [RequiredArgument]
            public InArgument<int> SecondNumber { get; set; }
            [Category("Output")]
            public OutArgument<int> ResultNumber { get; set; }
            protected override void Execute(CodeActivityContext context)
            {
            }
    //Note that these attributes are localized so you need to localize this attribute for Studio languages other than English
     
    //Dots allow for hierarchy. App Integration.Excel is where Excel activities are.
    [Category("Category.Where.Your.Activity.Appears.In.Toolbox")]
    [DisplayName("Human readable name instead of class name")]
    [Description("The text of the tooltip")]
     public class MathSqSum : CodeActivity
    {
     
        //Note that these attributes are localized so you need to localize this attribute for Studio languages other than English
                 [Category("Input")]
            [DisplayName("First Number")]
            [Description("Enter the first number")]
            [RequiredArgument]
            public InArgument<int> FirstNumber { get; set; }
            [Category("Input")]
            [DisplayName("Second Number")]
            [Description("Enter the second number")]
            [RequiredArgument]
            public InArgument<int> SecondNumber { get; set; }
            [Category("Output")]
            public OutArgument<int> ResultNumber { get; set; }
            protected override void Execute(CodeActivityContext context)
            {
            }
    

    <DisplayName(" ")>属性は、Studio の [プロパティ] パネルの入力フィールドの前に表示されるラベルです。<Description(" ")> 属性は、マウスをホバーしたときに表示されるツールチップのテキストです。

    カスタム アクティビティの [プロパティ] パネル

    宣言された要素を入力に必須にする場合は、 <ReguiredArgument> 属性が必要です。入力されていない場合は、アクティビティのタイトル バーに警告アイコンが表示されます。

  12. オーバーライドされた関数 <Execute( )> に機能を追加します。この場合、次のようになります。

    protected override void Execute(CodeActivityContext context)
    {
        var firstNumber = FirstNumber.Get(context);
      var secondNumber = SecondNumber.Get(context);
      
      var result = (int)Math.Pow(firstNumber + secondNumber, 2);
      ResultNumber.Set(context, result);
    }
    protected override void Execute(CodeActivityContext context)
    {
        var firstNumber = FirstNumber.Get(context);
      var secondNumber = SecondNumber.Get(context);
      
      var result = (int)Math.Pow(firstNumber + secondNumber, 2);
      ResultNumber.Set(context, result);
    }
    

(オプション)デザイナー インターフェイスを構築する

アクティビティにデザイナー インターフェイスを設定したくない場合は、[ライブラリの構築] セクションにスキップできます。

重要:

デザイナー インターフェイスを構築するには、 Windows Workflow Foundation コンポーネントが Visual Studio にインストールされている必要があります。Visual Studio インストーラーからコンポーネントを選択していない場合は、次の手順で追加できます。

  • Visual Studio で [ ツール ] メニューをクリックし、[ ツールと機能の取得] を選択します。[Visual Studio インストーラー] ウィンドウが表示されます。
  • [個々のコンポーネント] タブに切り替えて、Windows Workflow Foundation コンポーネントを検索します。これは [ 開発アクティビティ ] セクションの下にあります。
  • Windows Workflow Foundation コンポーネントの前にあるチェック ボックスをオンにして、[変更] をクリックします。必要なコンポーネントがインストールされます。

Visual Studio での Window Workflow Foundation コンポーネントの選択

  1. プロパティパネルからプロジェクトを右クリックします(この例では、プロジェクトはMathSquareOfSumです)。コンテキスト メニューが表示されます。

  2. [項目を追加] から [新しい項目...] を選択します。[ 新しい項目を追加 ] ウィンドウが表示されます。

  3. 左側のパネルの [インストール済み] カテゴリで [ワークフロー] を選択します。関連するすべての要素が表示されます。

  4. [ アクティビティ デザイナー ] を選択して [ 追加 ] をクリックし、項目をプロジェクトに含めます。

    [新しい項目を追加] ウィンドウでアクティビティ デザイナーを選択する。

    アクティビティ デザイナーの項目が追加され、対応する.xaml ファイルがすぐに開きます。次のようになります。

    アクティビティ デザイナーの項目の .xaml ファイルです。

  5. 既存のアクティビティ デザイナーのコードを次のように置き換えます。

    <sap:ActivityDesigner x:Class="MathSquareOfSum.MathSqSumDesigner"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
        xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation"
        xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">
        <a href="https://docs.uipath.com/ja/sap:ActivityDesigner.Resources">sap:ActivityDesigner.Resources</a>
            <ResourceDictionary>
                <sapc:ArgumentToExpressionConverter x:Key="ArgumentToExpressionConverter" />
            </ResourceDictionary>
        </sap:ActivityDesigner.Resources>
        <DockPanel Width="300">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="90"></ColumnDefinition>
                    <ColumnDefinition Width="210"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="First Number"></TextBlock>
                <sapv:ExpressionTextBox Grid.Row="0" Grid.Column="1"  OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="Enter first number" Expression="{Binding Path=ModelItem.FirstNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=In, Mode=TwoWay}" />
                <TextBlock Grid.Row="1" Grid.Column="0" Text="Second Number"></TextBlock>
                <sapv:ExpressionTextBox Grid.Row="1" Grid.Column="1"  OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="Enter second number" Expression="{Binding Path=ModelItem.SecondNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=In, Mode=TwoWay}" />
                <TextBlock Grid.Row="2" Grid.Column="0" Text="Result"></TextBlock>
                <sapv:ExpressionTextBox Grid.Row="2" Grid.Column="1"  OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="The sum of the numbers" UseLocationExpression="True" Expression="{Binding Path=ModelItem.ResultNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=Out, Mode=TwoWay}" />
            </Grid>
        </DockPanel>
    </sap:ActivityDesigner>
    <sap:ActivityDesigner x:Class="MathSquareOfSum.MathSqSumDesigner"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
        xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation"
        xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">
        <a href="https://docs.uipath.com/ja/sap:ActivityDesigner.Resources">sap:ActivityDesigner.Resources</a>
            <ResourceDictionary>
                <sapc:ArgumentToExpressionConverter x:Key="ArgumentToExpressionConverter" />
            </ResourceDictionary>
        </sap:ActivityDesigner.Resources>
        <DockPanel Width="300">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="90"></ColumnDefinition>
                    <ColumnDefinition Width="210"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Text="First Number"></TextBlock>
                <sapv:ExpressionTextBox Grid.Row="0" Grid.Column="1"  OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="Enter first number" Expression="{Binding Path=ModelItem.FirstNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=In, Mode=TwoWay}" />
                <TextBlock Grid.Row="1" Grid.Column="0" Text="Second Number"></TextBlock>
                <sapv:ExpressionTextBox Grid.Row="1" Grid.Column="1"  OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="Enter second number" Expression="{Binding Path=ModelItem.SecondNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=In, Mode=TwoWay}" />
                <TextBlock Grid.Row="2" Grid.Column="0" Text="Result"></TextBlock>
                <sapv:ExpressionTextBox Grid.Row="2" Grid.Column="1"  OwnerActivity="{Binding Path=ModelItem}" ExpressionType="s:Int32" HintText="The sum of the numbers" UseLocationExpression="True" Expression="{Binding Path=ModelItem.ResultNumber, Converter={StaticResource ArgumentToExpressionConverter},ConverterParameter=Out, Mode=TwoWay}" />
            </Grid>
        </DockPanel>
    </sap:ActivityDesigner>
    

    アクティビティに対して定義された新しいレイアウトは、次のようになります。

    アクティビティのレイアウトです。

  6. アクティビティ (この場合は ActMathSquareOfSum) を右クリックし、[ 追加 ] メニューの [ クラス...] を選択します。[ 新しい項目を追加 ] ウィンドウが表示されます。

  7. [クラス] 項目は既に選択されています。あとは、名前を DesignerMetadata.cs に変更して [ 追加] を選択するだけです。新しいクラスがアクティビティに追加され、新しいタブで開きます。

    [クラス] 項目の名前を DesignerMetadata.cs に変更します。

  8. 新しく作成した DesignerMetadata クラスに次の内容を追加します。

    using MathSquareOfSum;
    using System.Activities.Presentation.Metadata;
    using System.ComponentModel;
    namespace ActMathSquareOfSum
    {
        public class DesignerMetadata : IRegisterMetadata
        {
            public void Register()
            {
                AttributeTableBuilder attributeTableBuilder = new AttributeTableBuilder();
                attributeTableBuilder.AddCustomAttributes(typeof(MathSqSum), new DesignerAttribute(typeof(MathSqSumDesigner)));
                MetadataStore.AddAttributeTable(attributeTableBuilder.CreateTable());
            }
        }
    }
    using MathSquareOfSum;
    using System.Activities.Presentation.Metadata;
    using System.ComponentModel;
    namespace ActMathSquareOfSum
    {
        public class DesignerMetadata : IRegisterMetadata
        {
            public void Register()
            {
                AttributeTableBuilder attributeTableBuilder = new AttributeTableBuilder();
                attributeTableBuilder.AddCustomAttributes(typeof(MathSqSum), new DesignerAttribute(typeof(MathSqSumDesigner)));
                MetadataStore.AddAttributeTable(attributeTableBuilder.CreateTable());
            }
        }
    }
    
  • (オプション)デザイナー インターフェイスを構築する

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

接続

ヘルプ リソース サポート

学習する UiPath アカデミー

質問する UiPath フォーラム

最新情報を取得