它表示一个活动,它是一个抽象类。
它有一个Implementtation
属性,它的定义如下:
protected virtual Func<System.Activities.Activity> Implementation { get; set; }
它表示此Activity
对象要执行的活动,下面的例子,我们创建一个自定义的活动,它执行WriteLine
活动:
public sealed class ActivityTest1 : Activity { public ActivityTest1() { Implementation = (() => new Sequence { Activities = { new WriteLine { Text= "我的测试" } } }); } }
对于一个Activity
,它分别有children,importedChildren和implementtation
Child - 表示公开的子活动,可通过AddChild
方法进行添加
ImportedChild - 将一个子活动导入,这个Activity必须是被导入活动的子类(存疑)
ImplementtationChild - 实现子活动,它不是公共的,不必显示出来,表示的通常是后台子活动。
类实现部分 [Designer(typeof(IfTrueDesigner))] public sealed class IfTrue : NativeActivity { public Activity Body { get; set; } [RequiredArgument] public InArgument<bool> IfCondition { get; set; } protected override void Execute(NativeActivityContext context) { bool con = IfCondition.Get(context); Console.WriteLine("IfCondition值:" + con); if (con) { context.ScheduleActivity(Body); } } protected override void CacheMetadata(NativeActivityMetadata metadata) { metadata.AddChild(Body); base.CacheMetadata(metadata); } } // 视图部分 <sap:ActivityDesigner.Resources> <sadc:ArgumentToExpressionConverter x:Key="argConverter"></sadc:ArgumentToExpressionConverter> <DataTemplate x:Key="Collapsed"> <StackPanel> <TextBlock><Run Text="This is the collapsed view"/></TextBlock> </StackPanel> </DataTemplate> <DataTemplate x:Key="Expanded"> <StackPanel> <TextBlock>条件:</TextBlock> <sapv:ExpressionTextBox Content="{x:Null}" Expression="{Binding Path=ModelItem.IfCondition, Mode=TwoWay, Converter={StaticResource argConverter}, ConverterParameter=In}" OwnerActivity="{Binding ModelItem}" /> <sap:WorkflowItemPresenter Item="{Binding ModelItem.Body, Mode=TwoWay}" HintText="Please drop an activity here" /> </StackPanel> </DataTemplate> <Style x:Key="ExpandOrCollapsedStyle" TargetType="{x:Type ContentPresenter}"> <Setter Property="ContentTemplate" Value="{DynamicResource Collapsed}"/> <Style.Triggers> <DataTrigger Binding="{Binding ShowExpanded}" Value="true"> <Setter Property="ContentTemplate" Value="{DynamicResource Expanded}"/> </DataTrigger> </Style.Triggers> </Style> </sap:ActivityDesigner.Resources> <Grid> <ContentPresenter Style="{DynamicResource ExpandOrCollapsedStyle}" Content="{Binding}" /> </Grid>
对于这个例子,我们必须使用.AddChild方法,以将我们拖动进来的组件添加到IfTrue
组件的Children集合中,然后我们可以使用context.ScheduleActivity(Body)
来执行此子活动。
// 代码逻辑部分 [Designer(typeof(Delay2ActivityView))] public sealed class Delay2Activity : NativeActivity { public Activity Body { get;set; } private Activity a; protected override void CacheMetadata(NativeActivityMetadata metadata) { Variable<int> iv = new Variable<int>("i", 0); a = new While { Variables = { iv }, Condition = new GreaterThan<int, int, bool> { Left = 3, Right = new InArgument<int>((ActivityContext ctx) => iv.Get(ctx)), }, Body = new Sequence { Activities = { this.Body, new Delay { Duration = TimeSpan.FromSeconds(5) }, new WriteLine { Text = "延迟5s" }, new Assign<int> { To = iv, Value = new InArgument<int>((ActivityContext ctx) => iv.Get(ctx) + 1) } } } }; metadata.AddImplementationChild(a); base.CacheMetadata(metadata); } protected override void Execute(NativeActivityContext context) { context.ScheduleActivity(a); //UiPath.Core.Activities.Design.RetryDesigner } } // 视图部分 <sap:ActivityDesigner.Resources> <DataTemplate x:Key="Collapsed"> <StackPanel> <TextBlock><Run Text="This is the collapsed view"/></TextBlock> </StackPanel> </DataTemplate> <DataTemplate x:Key="Expanded"> <StackPanel> <sap:WorkflowItemPresenter Item="{Binding ModelItem.Body, Mode=TwoWay}" HintText="Please drop an activity here" /> </StackPanel> </DataTemplate> <Style x:Key="ExpandOrCollapsedStyle" TargetType="{x:Type ContentPresenter}"> <Setter Property="ContentTemplate" Value="{DynamicResource Collapsed}"/> <Style.Triggers> <DataTrigger Binding="{Binding ShowExpanded}" Value="true"> <Setter Property="ContentTemplate" Value="{DynamicResource Expanded}"/> </DataTrigger> </Style.Triggers> </Style> </sap:ActivityDesigner.Resources> <Grid> <ContentPresenter Style="{DynamicResource ExpandOrCollapsedStyle}" Content="{Binding}" /> </Grid>