WF4.0 应用篇(三) IActivityTemplateFactory
System.Activities.Presentation.IActivityTemplateFactory 模板
如果不想让用户设置Activity的所有属性,而希望很多属性都已按默认方式设置完成,可以使用IActivityTemplateFactory
简单的说,它就象是预先配置好的开箱即用的活动。一个简单的例子就是VS工具箱中Messaging分类下的ReceiveAndSendReply活动。但在System.Activities 程序集中并没有这个活动,它只是被称作ReceiveAndSendReply的IActivityTemplate。
接口
public interface IActivityTemplateFactory { Activity Create(DependencyObject target); } |
在Create方法中,可以创建预先配置好参数与属性的Activity
基本使用
myActivityA
public sealed class myActivityA : System.Activities.Presentation.IActivityTemplateFactory {
public Activity Create(DependencyObject target) { System.Activities.Statements.Sequence sequence = new System.Activities.Statements.Sequence(); sequence.DisplayName = "wxwinter sequence";
System.Activities.Statements.WriteLine writeLine = new System.Activities.Statements.WriteLine(); writeLine.Text = new InArgument<string>("hello wxwinter"); writeLine.DisplayName = "wxwinter writeline";
sequence.Activities.Add(writeLine);
return sequence; } } |
myActivityA 会出现在工具栏上
在设计器中使用
添加变量
public sealed class myActivityB : System.Activities.Presentation.IActivityTemplateFactory {
public Activity Create(DependencyObject target) {
var v1 = new Variable<string>() { Name = "v1", Default = "wxd" }; var v2 = new Variable<string>() { Name = "v2", Default = "wxwinter" }; var v3 = new Variable<string>() { Name = "v3", Default = "lzm" }; return new Sequence { Variables = { v1,v2,v3 }, Activities = { new WriteLine { Text = new VisualBasicValue<string>("v1") }
} }; }
} |
改变设计器字体颜色
public class myActivityC : System.Activities.Presentation.IActivityTemplateFactory {
public Activity Create(DependencyObject target) { //target is System.Activities.Presentation.View.ActivityTypeDesigner // System.Activities.Presentation.View.ActivityTypeDesigner is System.Activities.Presentation.View.DesignerWithHeader // System.Activities.Presentation.View.DesignerWithHeader is System.Activities.Presentation.WorkflowViewElement
System.Activities.Presentation.WorkflowViewElement workflowViewElement = target as WorkflowViewElement;
workflowViewElement.Foreground = System.Windows.Media.Brushes.Red;
return new Sequence(); } } |
添加参数
public class myActivityD : System.Activities.Presentation.IActivityTemplateFactory { public Activity Create(DependencyObject target) { //target is System.Activities.Presentation.View.ActivityTypeDesigner // System.Activities.Presentation.View.ActivityTypeDesigner is System.Activities.Presentation.View.DesignerWithHeader // System.Activities.Presentation.View.DesignerWithHeader is System.Activities.Presentation.WorkflowViewElement
System.Activities.Presentation.WorkflowViewElement workflowViewElement = target as WorkflowViewElement;
var k = workflowViewElement.ModelItem.Root.Properties["Properties"].Collection;
k.Add(new DynamicActivityProperty { Name = "wxd", Type = typeof(InArgument<string>), Value = new InArgument<string>() });
return new Sequence {
Activities = { new WriteLine { Text = new VisualBasicValue<string>("wxd") }
} }; } } |