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

コードを使用してアクティビティを作成する (レガシ)

大事な: このページの情報は、非推奨の .NET Framework を対象とするアクティビティについて説明しています。 最新バージョンの Studio で .NET をターゲットとするアクティビティを作成する方法の詳細については、「 コードを使用してアクティビティを作成する」をご覧ください。
An activity is the building block of a process automation. UiPath Studio comes with various built-in dedicated activities (such as PDF, mail, Excel, SAP, and more) you can install through the Package Manager, depending on your needs. You can read The UiPath Activities Guide for more details and examples, as well as how to Manage Activities Packages . Additionally, you can create custom activities to better automate processes based on your needs.

カスタム アクティビティを作成するための 2 つの主要な手順は以下のとおりです。

  1. カスタム アクティビティコードを記述します。
  2. UiPath に外部アセンブリ (.dll) を追加します。
注:

カスタム アクティビティを作成するには、次のコンポーネントが必要です。

カスタム アクティビティの作成

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

  1. Microsoft Visual Studio を起動します。
  2. [ファイル] > [新規] > [プロジェクト] をクリックします (ショートカット: Ctrl + Shift + N キー)。[新しいプロジェクト] ウィンドウが表示されます。
  3. [ 言語 ] ドロップダウン メニューから [ C# ] を選択します。C# を使用するすべての依存関係のリストが表示されます。
  4. [クラス ライブラリ (.NET Framework)] を選択します。これは、カスタム アクティビティを .dll ファイルとしてエクスポートする場合に役立ちます。 [ 次へ ] をクリックして 、[新しいプロジェクトを設定 ] ウィンドウに移動します。


  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 に移行する」をご覧ください。


  1. [ 作成 ] をクリックしてコード デザイナーに移動し、アクティビティ コードの書き込みを開始します。
  2. [ ソリューション エクスプローラー ] パネルで [ 参照 ] 分岐を右クリックして、[ 参照の追加...] を選択します。 [ 参照マネージャー ] ウィンドウが表示されます。


  3. System.Activities および System.ComponentModel.Composition 参照を検索し、追加します。各ボックスの前にあるボックスをオンにして、[OK] をクリック します。 そうすることで、前述の参照のクラスを使用できます。




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

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

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



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

    //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 コンポーネントの前にあるボックスをチェックし、[ 変更] をクリックします。必要なコンポーネントがインストールされます。


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



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

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

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



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


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

<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="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="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>

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



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

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


20. 新しく作成される 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());
        }
    }
}

ライブラリを構築する

プロジェクトのビルドを試す前に、必ずプロジェクトでエラーがないかテストしてください。

[ビルド ] メニューから [ ソリューションを構築] を選択します。 MathSquareOfSum.dll ライブラリが構築され、以下の場所に保存されるようになりました。
%UserProfile%\source\repos\MathSquareOfSum\MathSquareOfSum\bin\Debug

ライブラリが構築されると、以下に示すメッセージが [ 出力] パネルに表示されます。



NuGet パッケージの作成

  1. NuGet Package Explorer を起動し、[ Create a new package] (Ctrl + N)をクリックします。 [パッケージのメタデータ ] と [パッケージの 内容] を示す分割ウィンドウが表示されます。後者のセクションにすべての依存関係を追加する必要があります。
  2. [ Package contents ] セクション内を右クリックします。 コンテキスト メニューが表示されます。
  3. [Add lib folder] をクリックします。新しい [lib] 項目が、[Package contents] セクションに作成されます。


  4. [lib] を右クリックして、[Add Existing File…] を選択します。



  5. 上記で作成した外部アセンブリ (.dll) を読み込みます。 この例では、 MathSquareOfSum.dll ファイルは %UserProfile%\source\repos\MathSquareOfSum\MathSquareOfSum\bin\Debug フォルダーにあります。
  6. ファイルを選択して 、[編集] メニューにアクセスし、[ メタデータの編集] を選択します。 左側のパネルには、編集可能なメタデータ フィールドが表示されます。


  7. カスタム アクティビティをより適切に説明するためにフィールドに入力します。
  8. [ ID ] フィールドに入力します。 この例では、 UiPath.MathSquare.Activitiesを指定できます。最初の値は UiPathであり、パッケージを作成したチームを意味しています。 特定のチームが関与しなかった場合、最初の値を空白のままにできます。



  9. [Package metadata ] セクションの左上隅にある緑色のチェック マーク ボタンをクリックして、すべての変更を保存します。
  10. [ファイル ] メニューから [名前を付けて保存...] を選択します。エクスプローラー ウィンドウが表示され、新しいファイルの場所を選択できます。
  11. ローカル パッケージが UiPath が保存するフォルダーにファイルを保存します (2021.4 より前のバージョンでC:\Program Files (x86)\UiPath\Studio\Packages するか、バージョン 2021.4 以降 C:\Program Files\UiPath\Studio\Packages )。 UiPath.MathSquare.Activities.1.0.0.nupkg ファイルがパッケージ フォルダー内の UiPath.MathSquare.Activities フォルダーに作成されます。

Studio に NuGet パッケージを読み込む

  1. Studio から パッケージ マネージャー にアクセスします。
  2. NuGet パッケージをローカル パッケージ フォルダーに保存しているため、アクティビティは ローカル フィードで利用可能である必要があります。


  3. アクティビティを検索し、[ インストール ]、[ 保存]の順にクリックします。 新しいアクティビティがインストールされ、オートメーション プロジェクトで使用できるようになりました。

Was this page helpful?

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