2. DependencyProperty事件只能绑定到事件,而不能绑定到委托
DependencyProperty事件只能赋值到委托
3. 即使用+=方式为事件添加订阅,原有的绑定也会被移除
说明: [事件A] 绑定到 [事件B] [事件A] + = [委托1]; [事件B] + = [委托2]; 触发[[事件A],将执行 [委托1],不会执行[委托2],绑定已被清除 |
4. 多个绑定都会被执行
A节点.A事件 += [委托1] B节点.B事件 绑定到 [A.A事件] C节点.C事件 绑定到 [A.A事件] 执行A、B、C节点,[委托1]会被执行3次 |
1.1 AddHandler、RemoveHandler 通过从属属性添加、移除事件定阅
System.Workflow.ComponentModel.DependencyObject的实例方法
public void AddHandler(DependencyProperty dependencyEvent, object value); 实例.AddHandler(类.DependencyProperty事件,委托) |
public void RemoveHandler(DependencyProperty dependencyEvent, object value); 实例.RemoveHandler(类.DependencyProperty事件,委托) |
this.AddHandler(EAEvent, new System.EventHandler(OnEA)); |
1.2 定义一个有DependencyProperty事件的Activity
public class Activity1:Activity { //为事件绑定操作 protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { //第一种绑定方式 this.AddHandler(Activity1.EAEvent, new System.EventHandler(OnEA)); //第二种绑定方式 //this.EA += new System.EventHandler(OnEA);
return base.Execute(executionContext); } //事件要执行的操作 void OnEA(object sender, EventArgs e) { System.Console.WriteLine("EA"); } //定义DependencyProperty事件 public static DependencyProperty EAEvent = DependencyProperty.Register("EA", typeof(EventHandler), typeof(Activity1)); //定义事件 public event EventHandler EA { add { base.AddHandler(Activity1.EAEvent, value); } remove { base.RemoveHandler(Activity1.EAEvent, value); } } } |
事件的绑定也可在类外部进行 private Activity1 activity11; this.activity11 = new Activity1(); this.activity11.EA += new EventHandler(activity11_EA); this.activity11.AddHandler(Activity1.EAEvent, new System.EventHandler(activity11_EA)); |
1.3 SetBinding代码方式的绑定
详细见DependencyProperty从属属性
System.Workflow.ComponentModel.ActivityBind activitybind1 = new System.Workflow.ComponentModel.ActivityBind(); activitybind1.Name = "codeActivity3"; activitybind1.Path = "ExecuteCode"; this.activity11.SetBinding(WorkflowConsoleApplication2.Activity1.EAEvent, activitybind1); |
1.4 VS设计器进行绑定
略
1.5 XOML配置文件的从属属性绑定
详细见DependencyProperty从属属性
<CodeActivityx:Name="codeActivity1"ExecuteCode="{ActivityBind activity11,Path=EA}" /> <CodeActivityx:Name="codeActivity2"ExecuteCode="codeActivity2_ExecuteCode" /> |