Partager via


Tâche 2 : créer l'application hôte d'activité personnalisée

Dans cette tâche, vous allez créer l'application hôte utilisée dans ce didacticiel.

L'application que vous générez pour le didacticiel est un navigateur Web textuel construit à l'aide d'un Windows Form. Le formulaire contient une zone de texte, utilisée pour entrer une adresse de page Web et un bouton, nommé Go. Lorsque le bouton est activé, l'application charge et démarre le workflow séquentiel. Ce workflow contient une activité personnalisée unique qui effectue la tâche de téléchargement du texte de la page Web.

NoteRemarque :

Bien qu'il soit conseillé de suivre les exercices de façon linéaire, ce n'est pas obligatoire. Vous pouvez démarrer cet exercice en ouvrant l'exemple de projet et en continuant avec les étapes de la section suivante.

Si vous utilisez Visual Studio, le fichier du code source (program1.vb ou program1.cs) a été créé automatiquement pour vous dans la tâche 1. Suivez ces étapes pour adapter le fichier en vue de l'utiliser dans cet exemple.

Pour créer le fichier de code source Windows Form dans Visual Studio

  1. Dans l'Explorateur de solutions, cliquez avec le bouton droit sur program1, puis sélectionnez Renommer. Remplacez son nom par CustomActivityHost.

  2. Dans l'Explorateur de solutions, double-cliquez sur CustomActivityHost pour l'ouvrir dans l'éditeur de code dans le volet gauche. Ajoutez le code suivant pour l'application Windows Form.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Workflow.Runtime;
    
    namespace Microsoft.Samples.Workflow.Tutorials.CustomActivity
    {
        public class MainForm : Form
        {
            private System.Windows.Forms.Label addressCaption;
            private System.Windows.Forms.TextBox address;
            private System.Windows.Forms.TextBox data;
            private System.Windows.Forms.Button goButton;
            private System.ComponentModel.IContainer components = null;
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void goButton_Click(object sender, EventArgs e)
            {
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            private void InitializeComponent()
            {
                this.addressCaption = new System.Windows.Forms.Label();
                this.address = new System.Windows.Forms.TextBox();
                this.data = new System.Windows.Forms.TextBox();
                this.goButton = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // addressCaption
                // 
                this.addressCaption.AutoSize = true;
                this.addressCaption.Location = new System.Drawing.Point(15, 13);
                this.addressCaption.Name = "addressCaption";
                this.addressCaption.Size = new System.Drawing.Size(41, 13);
                this.addressCaption.TabIndex = 0;
                this.addressCaption.Text = "Address";
                // 
                // address
                // 
                this.address.Anchor = ((System.Windows.Forms.AnchorStyles)
                    (((System.Windows.Forms.AnchorStyles.Top |
                    System.Windows.Forms.AnchorStyles.Left) |
                    System.Windows.Forms.AnchorStyles.Right)));
                this.address.Location = new System.Drawing.Point(16, 28);
                this.address.Name = "address";
                this.address.Size = new System.Drawing.Size(430, 20);
                this.address.TabIndex = 0;
                this.address.Text = "http://";
                // 
                // data
                // 
                this.data.Anchor = ((System.Windows.Forms.AnchorStyles)
                    ((((System.Windows.Forms.AnchorStyles.Top |
                    System.Windows.Forms.AnchorStyles.Bottom) |
                    System.Windows.Forms.AnchorStyles.Left) |
                    System.Windows.Forms.AnchorStyles.Right)));
                this.data.Location = new System.Drawing.Point(15, 55);
                this.data.Multiline = true;
                this.data.Name = "data";
                this.data.ReadOnly = true;
                this.data.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                this.data.Size = new System.Drawing.Size(496, 320);
                this.data.TabIndex = 2;
                // 
                // goButton
                // 
                this.goButton.Location = new System.Drawing.Point(462, 25);
                this.goButton.Name = "goButton";
                this.goButton.Size = new System.Drawing.Size(49, 23);
                this.goButton.TabIndex = 1;
                this.goButton.Text = "Go";
                this.goButton.Anchor = ((System.Windows.Forms.AnchorStyles)
                    ((System.Windows.Forms.AnchorStyles.Top |
                    System.Windows.Forms.AnchorStyles.Right)));
                this.goButton.Click += new System.EventHandler(this.goButton_Click);
                // 
                // MainForm
                // 
                this.AcceptButton = this.goButton;
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(523, 407);
                this.Controls.Add(this.goButton);
                this.Controls.Add(this.data);
                this.Controls.Add(this.address);
                this.Controls.Add(this.addressCaption);
                this.Name = "MainForm";
                this.Text = "Web Tear";
                this.ResumeLayout(false);
                this.PerformLayout();
            }
        }
    
        static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.Run(new MainForm());
            }
        }
    }
    

Pour créer le fichier de code source Windows Form à l'aide d'un éditeur de texte

  1. Dans votre fichier projet principal (CustomActivity), insérez un nouvel élément ItemGroup avant l'élément Import à la fin du fichier.

  2. Dans l'élément ItemGroup, ajoutez un nouvel élément Compile.

  3. Ajoutez un nouvel attribut à l'élément Compile nommé Include à l'aide du nom de fichier CustomActivityHost.cs pour les projets C# ou les CustomActivityHost.vb pour les projets Visual Basic.NET.

  4. Ajoutez un nouvel élément enfant à l'élément Compile, nommé SubType.

    Affectez la valeur Formà cet élément. Votre nœud ItemGroup s'affichera comme suit :

    <ItemGroup>
        <Compile Include="CustomActivityHost.vb">
            <SubType>Form</SubType>
        </Compile>
    </ItemGroup>
    
    <ItemGroup>
        <Compile Include="CustomActivityHost.cs">
            <SubType>Form</SubType>
        </Compile>
    </ItemGroup>
    

Pour ajouter le code Windows Form à l'application hôte

  1. Dans votre répertoire de projet, créez un nouveau fichier nommé CustomActivityHost.

    Attribuez au fichier une extension .cs si vous créez une application C# ou .vb si vous créez une application Visual Basic.

  2. Dans le fichier de code source CustomActivityHost, ajoutez le code suivant pour l'application Windows Form.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Workflow.Runtime;
    
    namespace Microsoft.Samples.Workflow.Tutorials.CustomActivity
    {
        public class MainForm : Form
        {
            private System.Windows.Forms.Label addressCaption;
            private System.Windows.Forms.TextBox address;
            private System.Windows.Forms.TextBox data;
            private System.Windows.Forms.Button goButton;
            private System.ComponentModel.IContainer components = null;
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void goButton_Click(object sender, EventArgs e)
            {
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            private void InitializeComponent()
            {
                this.addressCaption = new System.Windows.Forms.Label();
                this.address = new System.Windows.Forms.TextBox();
                this.data = new System.Windows.Forms.TextBox();
                this.goButton = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // addressCaption
                // 
                this.addressCaption.AutoSize = true;
                this.addressCaption.Location = new System.Drawing.Point(15, 13);
                this.addressCaption.Name = "addressCaption";
                this.addressCaption.Size = new System.Drawing.Size(41, 13);
                this.addressCaption.TabIndex = 0;
                this.addressCaption.Text = "Address";
                // 
                // address
                // 
                this.address.Anchor = ((System.Windows.Forms.AnchorStyles)
                    (((System.Windows.Forms.AnchorStyles.Top |
                    System.Windows.Forms.AnchorStyles.Left) |
                    System.Windows.Forms.AnchorStyles.Right)));
                this.address.Location = new System.Drawing.Point(16, 28);
                this.address.Name = "address";
                this.address.Size = new System.Drawing.Size(430, 20);
                this.address.TabIndex = 0;
                this.address.Text = "http://";
                // 
                // data
                // 
                this.data.Anchor = ((System.Windows.Forms.AnchorStyles)
                    ((((System.Windows.Forms.AnchorStyles.Top |
                    System.Windows.Forms.AnchorStyles.Bottom) |
                    System.Windows.Forms.AnchorStyles.Left) |
                    System.Windows.Forms.AnchorStyles.Right)));
                this.data.Location = new System.Drawing.Point(15, 55);
                this.data.Multiline = true;
                this.data.Name = "data";
                this.data.ReadOnly = true;
                this.data.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                this.data.Size = new System.Drawing.Size(496, 320);
                this.data.TabIndex = 2;
                // 
                // goButton
                // 
                this.goButton.Location = new System.Drawing.Point(462, 25);
                this.goButton.Name = "goButton";
                this.goButton.Size = new System.Drawing.Size(49, 23);
                this.goButton.TabIndex = 1;
                this.goButton.Text = "Go";
                this.goButton.Anchor = ((System.Windows.Forms.AnchorStyles)
                    ((System.Windows.Forms.AnchorStyles.Top |
                    System.Windows.Forms.AnchorStyles.Right)));
                this.goButton.Click += new System.EventHandler(this.goButton_Click);
                // 
                // MainForm
                // 
                this.AcceptButton = this.goButton;
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(523, 407);
                this.Controls.Add(this.goButton);
                this.Controls.Add(this.data);
                this.Controls.Add(this.address);
                this.Controls.Add(this.addressCaption);
                this.Name = "MainForm";
                this.Text = "Web Tear";
                this.ResumeLayout(false);
                this.PerformLayout();
            }
        }
    
        static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.Run(new MainForm());
            }
        }
    }
    

Compilation du code

Pour plus d'informations sur la compilation du code, consultez Compilation du code.

Exécutez l'application dans le dossier \bin\debug (ou le dossier \bin si vous utilisez Visual Basic .NET). Votre application apparaîtra comme dans l'illustration suivante.

Sortie de la tâche 2

Dans Tâche 3 : créer le workflow séquentiel d'activités personnalisées, vous allez créer ultérieurement un workflow séquentiel de base à partir duquel vous pourrez procéder.

Exécutez l'application dans le dossier \bin\debug (ou le dossier \bin si vous utilisez Visual Basic .NET).

Voir aussi

Tâches

Tâche 3 : créer le workflow séquentiel d'activités personnalisées

Concepts

Création d'une application hôte de workflow

Autres ressources

Didacticiel : créer une activité personnalisée
Custom Activities

Footer image

Copyright ©2007 par Microsoft Corporation. Tous droits réservés.