Home > WF4

[WF4]Create A Custom Activity

16. March 2010

One of the great feature of WF4 is that we can create our own Activity (and its surface) very easy. For example, let's create a PersonInfoActivity:
You will see this in workflow:


But, can we make it more interesting? Say, let it looks like this:

To do this: we need to create an Activity Designer, which is actually a WPF xaml file.

<sap:ActivityDesigner x:Class="MyActivityDesignerLibrary.PersonInfoActivityDesigner"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"

    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"

    xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation"

    xmlns:s="clr-namespace:System;assembly=mscorlib"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >

    <sap:ActivityDesigner.Resources>

        <!--<ResourceDictionary>

            <sapc:ArgumentToExpressionConverter x:Key="ArgumentToExpressionConverter"/>

        </ResourceDictionary>-->

        <sapc:ArgumentToExpressionConverter x:Key="ArgumentToExpressionConverter"/>

        <DataTemplate x:Key="Collapsed">

            <StackPanel>

                <TextBlock>This is the collapsed view</TextBlock>

            </StackPanel>

        </DataTemplate>

        <DataTemplate x:Key="Expanded">

            <StackPanel>

                <Label Content="Name"/>

                <sapv:ExpressionTextBox

                      HintText="Name"

                      Expression="{Binding Path=ModelItem.name,

                                           Mode=TwoWay,

                                           Converter={StaticResource ArgumentToExpressionConverter },

                                           ConverterParameter=In}"

                      OwnerActivity="{Binding Path=ModelItem}"

                      ExpressionType="s:String"/>

                <Label Content="Age"/>

                <sapv:ExpressionTextBox

                      HintText="Age"

                      Expression="{Binding Path=ModelItem.age,

                                         Mode=TwoWay,

                                         Converter={StaticResource ArgumentToExpressionConverter },

                                         ConverterParameter=In}"

                      OwnerActivity="{Binding Path=ModelItem}"

                      ExpressionType="s:Int32"/>

            </StackPanel>

        </DataTemplate>

        <Style x:Key="ExpandOrCollapsedStyle"

               TargetType="{x:Type ContentPresenter}">

            <Setter Property="ContentTemplate"

                    Value="{DynamicResource Collapsed}"/>

            <Style.Triggers>

                <DataTrigger Binding="{Binding Path=ShowExpanded}" Value="true">

                    <Setter Property="ContentTemplate" Value="{DynamicResource Expanded}"/>

                </DataTrigger>

            </Style.Triggers>

        </Style>

    </sap:ActivityDesigner.Resources>

    <Grid>

        <ContentPresenter Style="{DynamicResource ExpandOrCollapsedStyle}" Content="{Binding}" />

    </Grid>

</sap:ActivityDesigner>

It is like a face of our activity. But, how could the activity know which face it belong to? We place this attribute
    [Designer(typeof(PersonInfoActivityDesigner))]
Ahead of the PersonInfoActivity class:

Ok, that is all we need. This is the final structure:

WF4

Comments

Sofia
Sofia
5/24/2011 6:16:07 AM #
Hello
How can I download the source code?
Thank you
andrew
andrew
5/24/2011 11:54:05 PM #
You can copy the code from page and paste in vs2010 directly.
10/13/2011 6:14:02 PM #
Thanks for your infor
11/15/2011 7:20:41 PM #
Comparing with other website, I conjectured that UK based mobile phone shopping would be very helpful for all because it website cheap mobile phone deals on orange, 02, and Vodafone.
12/8/2011 1:39:33 AM #
<p><a href="http://www.womens-ugg-boots.org/">Womens Ugg Boots</a> 'Five per cent of ninety pounds is <p><a href="http://www.cheap-ugg-boot.net/">Cheap Ugg Boot</a> four pounds ten shillings,' <p><a href="http://www.cheap-ugg-boot.net/">Discount Ugg Boots</a> said Mr Levy cheerfully.<p><a href="http://www.womens-ugg-boots.org/">Discount Uggs Boots</a> 'You can pay now or on receipt of your first term's salary. <p><a href="http://www.womens-ugg-boots.org/"> Ugg Outlet</a> If you pay now there is a reduction of 15 per cent.<p><a href="http://www.womens-ugg-boots.org/">Cheap Uggs Boots On Sale</a> That would be three pounds six shillings and sixpence.''I'll pay you when I get my wages,' <p><a href="http://www.cheap-ugg-boot.net/"> Womens Winter Boots</a> said Paul.'Just as you please,' <p><a href="http://www.cheap-ugg-boot.net/">Sheepskin Boots On Sale</a> said Mr Levy. 'Only too <p><a href="http://www.brand-dress.com/">Brand Dress</a> glad to have been of use to you.'
1/18/2012 5:14:19 AM #
I have surfed the net more than three hours today, and your blog was the coolest of all.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading