15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Custom activities ❘ OC293<br />

{<br />

}<br />

if (!(act is SequenceActivity))<br />

return false;<br />

}<br />

return base.CanInsertActivities(insertLocation, activitiesToInsert);<br />

}<br />

protected override CompositeActivity OnCreateNewBranch()<br />

{<br />

return new SequenceActivity();<br />

}<br />

This Designer derives from ParallalActivityDesigner, which provides you with good design-time<br />

behavior when adding child activities. You will need to override CanInsertActivities to return false if<br />

any of the dropped activities is not a SequenceActivity. If all activities are of the appropriate type, you<br />

can call the base class method, which makes some further checks on the activity types permitted within<br />

your custom activity.<br />

You should also override the OnCreateNewBranch method that is called when the user chooses the Add<br />

Branch menu item. The Designer is associated with the activity by using the [Designer] attribute, as<br />

shown here:<br />

[Designer(typeof(DaysOfWeekDesigner))]<br />

public class DaysOfWeekActivity: CompositeActivity<br />

{<br />

}<br />

The design-time behavior is nearly complete; however, you also need to add a class that is derived from<br />

ActivityToolboxItem to this activity, because that defines what happens when an instance of that activity<br />

is dragged from the toolbox. The default behavior is simply to construct a new activity; however, in the<br />

example you also want to create two default branches. The following code shows the toolbox item class in<br />

its entirety:<br />

[Serializable]<br />

public class DaysOfWeekToolboxItem: ActivityToolboxItem<br />

{<br />

public DaysOfWeekToolboxItem(Type t)<br />

: base(t)<br />

{<br />

this.DisplayName = "DaysOfWeek";<br />

}<br />

}<br />

private DaysOfWeekToolboxItem(SerializationInfo info,<br />

StreamingContext context)<br />

{<br />

this.Deserialize(info, context);<br />

}<br />

protected override IComponent[] CreateComponentsCore(IDesignerHost host)<br />

{<br />

CompositeActivity parent = new DaysOfWeekActivity();<br />

parent.Activities.Add(new SequenceActivity());<br />

parent.Activities.Add(new SequenceActivity());<br />

}<br />

return new IComponent[] { parent };<br />

As shown in the code, the display name of the activity was changed, a serialization constructor was<br />

implemented, <strong>and</strong> the CreateComponentsCore method was overridden.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!