sdk
latest
false
Wichtig :
Bitte beachten Sie, dass dieser Inhalt teilweise mithilfe von maschineller Übersetzung lokalisiert wurde. Es kann 1–2 Wochen dauern, bis die Lokalisierung neu veröffentlichter Inhalte verfügbar ist.
UiPath logo, featuring letters U and I in white

Entwickleranleitung

Letzte Aktualisierung 30. Okt. 2025

Writing the code for a custom activity

Um besser zu verstehen, wie der Code für eine benutzerdefinierte Aktivität geschrieben wird, erstellen wir eine einfache Aktivität, die den Benutzer nach zwei Zahlen fragt und dann die Quadratur ihrer Summe ausgibt.
  1. Microsoft Visual Studio starten (Launch Microsoft Visual Studio).
  2. Select File > New >Project… (shortcut: Ctrl + Shift + N). The New Project window is displayed.
  3. Wählen Sie C# aus dem Dropdown-Menü Sprachen aus. Die Liste aller Abhängigkeiten, die C# verwenden, wird angezeigt.
  4. Select Class Library (.NET Framework). This helps us export the custom activity as a .dll file. Select Next to go to the Configure your new project window.


  5. Fill in the Project name field with the desired activity name. In our case, we can use "MathSquareOfSum".
  6. Wählen Sie .NET Framework 4.6.1 aus dem Dropdown-Menü Framework aus. Dadurch wird sichergestellt, dass die Bibliothek mit UiPath Studio kompatibel ist.
    Wichtig: In Windows-Legacy-Projekten unterstützt UiPath Studio Aktivitäten, die mit .NET Framework 4.5.2 bis 4.6.1 erstellt wurden. Informationen zum Migrieren von Aktivitäten zu .NET zur Verwendung in Projekten mit Windows-Kompatibilität finden Sie unter Migrieren von Aktivitäten zu .NET 6.


  7. Select Create to go to the code designer and start writing the activity code.
  8. Klicken Sie im Projektmappen- Explorer mit der rechten Maustaste auf die Verzweigung Referenzen , und wählen Sie Referenz hinzufügen...aus. Das Fenster Referenzmanager wird angezeigt.
  9. Suchen Sie nach den Referenzen System.Activities und System.ComponentModel.Composition und fügen Sie sie hinzu. Aktivieren Sie das jeweilige Kontrollkästchen und klicken Sie auf OK. Dadurch können wir Klassen aus den oben genannten Referenzen verwenden.




  10. Jetzt müssen wir sicherstellen, dass unser Code die neu hinzugefügten Verweise verwendet. Dazu fügen Sie im Code-Designer die folgenden Zeilen hinzu:
    using System.Activities;
    using System.ComponentModel;using System.Activities;
    using System.ComponentModel;
    Unser Projekt sollte nun folgendermaßen aussehen:


  11. Fügen Sie die Eingabe- und Ausgabeparameter hinzu. In unserem Fall sollte der Code folgendermaßen aussehen:
    //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)
            {
            }
    Das Attribut <DisplayName(" ")> ist die Beschriftung, die vor dem Eingabefeld im Eigenschaftenbereich in Studio angezeigt wird. Das <Description(" ")> -Attribut ist der Text der QuickInfo, die beim Darüberfahren mit der Maus angezeigt wird.


    The <ReguiredArgument> attribute is needed if you want a declared element to be mandatory for input. If it is not filled in, a caution icon appears in the title bar of the activity.
  12. Add the functionality in the <Execute( )> overridden function. In our case, it looks like this:
    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);
    }

(Optional) Building a Designer Interface

Wenn Sie keine Designeroberfläche für Ihre Aktivität möchten, können Sie zum Abschnitt Bibliothekserstellung springen.

Wichtig:

Um eine Designeroberfläche zu erstellen, muss die Komponente „Windows Workflow Foundation“ in Visual Studio installiert sein. Wenn Sie die Komponente nicht aus dem Visual Studio-Installationsprogramm ausgewählt haben, können Sie sie wie folgt hinzufügen:

  • Klicken Sie in Visual Studio auf das Menü Extras und wählen Sie Tools und Funktionen abrufen...aus. Das Fenster Visual Studio-Installer wird angezeigt.
  • Wechseln Sie zur Registerkarte Einzelne Komponenten und suchen Sie nach der Komponente Windows Workflow Foundation . Sie befindet sich im Abschnitt Entwicklungsaktivitäten .
  • Aktivieren Sie das Kontrollkästchen vor der Komponente „ Windows Workflow Foundation“ und klicken Sie auf Ändern. Die erforderliche Komponente wird installiert.


  1. Right-click the project from the Properties panel (in our case, the project is MathSquareOfSum). The context menu shows up.
  2. From the Add item, select New Item.... The Add New Item window is displayed.
  3. Select Workflow under the Installed category on the left panel. All related elements are displayed.
  4. Select Activity Designer and click Add to include the item in the project.


    Das Aktivitätsdesigner -Element wird hinzugefügt und die entsprechende .xaml -Datei wird sofort geöffnet. Sie sollte folgendermaßen aussehen:


  5. Replace the existing Activity Designer code with the following:
    <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>
    Das neu definierte Layout für die Aktivität sollte nun folgendermaßen aussehen:


  6. Right-click the activity (in our case ActMathSquareOfSum), and from the Add menu, select Class.... The Add New Item window is displayed.
  7. The Class item is already selected. All that is needed now is to rename it to DesignerMetadata.cs, and select Add. The new class is now added to the activity and opens up in a new tab.


  8. Add the following content in the newly-created DesignerMetadata class:
    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());
            }
        }
    }
  • (Optional) Building a Designer Interface

War diese Seite hilfreich?

Hilfe erhalten
RPA lernen – Automatisierungskurse
UiPath Community-Forum
Uipath Logo
Vertrauen und Sicherheit
© 2005–2025 UiPath. Alle Rechte vorbehalten