Tuesday, September 4, 2007

Designing and Building a CAB Application

1. Create the shell and the form, and displaying the form
2. creating and loading the module
3. Adding the tabbed workspace to the form and running the module
4. Creating and adding the SmartPart user control to the module, and displaying the results through this SmartPart


Creating the Shell and the Form

1. Application name -> ShellApplication

2. Form1.cs -> ShellForm.cs

3. Add References
Microsoft.Practices.CompositeUI.dll
Microsoft.Practices.CompositeUI.WinForms.dll
Microsoft.Practices.ObjectBuilder.dll
Adding a new WorkItem

4. Add New class named ShellWorkItem.cs
Using Microsoft.Practices.CompositeUI

5. Class should be public and inherit from WorkItem
public class ShellWorkItem : WorkItem
{
}
6. Rename Program.cs to ShellApplication.cs
using Microsoft.Practices.CompositeUI.WinForms;
7. Replace the static class with public and inherit from FormShellApplication
public class ShellApplication : FormShellApplication<ShellWorkItem, ShellForm>
{
}
8. Add the [STAThread] attribute and intiate ShellApplication class and call Run method.
[STAThread]
static void Main()
{
new ShellApplication().Run();
}


Creating and Loading the Module

1. Create a new Class Library named MyModule

2. set Outputpath to ..\ShellApplication\bin\Debug (from project properties build output page)

3. Add reference
Microsoft.Practices.CompositeUI.dll
Microsoft.Practices.CompositeUI.WinForms.dll
Microsoft.Practices.ObjectBuilder.dll
4. Add a class named MyWorkItem.cs. Make it public and derive from WorkItem
using Microsoft.Practices.CompositeUI;
public class MyWorkItem : WorkItem
{
}
5. Add a class named MyModuleInit.cs and make it public and derive from ModuleInit. Add a variable myCatalogService reference the WorkItemTypeCatalogService. so that you can access it to register your WorkItem.
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Services;
using System.Windows.Forms;

public class MyModuleInit : ModuleInit
{
private IWorkItemTypeCatalogService myCatalogService;
}
6. Add the following public property
[ServiceDependency]
public IWorkItemTypeCatalogService myWorkItemCatalog
{
set { myCatalogService = value; }
}
7. Override the Load method of ModuleInit
public override void Load()
{
base.Load();
myCatalogService.RegisterWorkItem<MyWorkItem>();
}
8. Add a new XML file named ProfileCatalog.xml
<?xml version="1.0" encoding="utf-8" ?>
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile">
<Modules>
<ModuleInfo AssemblyFile="MyModule.dll" />
</Modules>
</SolutionProfile>
9. select property of ProfileCatalog.xml and change the Copy To Output Directory property to Copy Always


Adding the TabWorkspace

1. Open the ShellForm.cs and drag a SplitContainer control onto the form

(Note: right click the Toolbox, click items.. and then browse and select
Microsoft.Practices.CompositeUI.Winforms.dll)
2. Drag a TabWorkSpace onto the form and drop it onto the left hand panel of the SplitContainer.

3. Right click the TabWorkSpace and click Remove Tab.


Creating and Showing the SmartPart

Create an Interface

1. Add a new Interface named IMyView.cs and make it public
public interface IMyView
{
event EventHandler Load;
string Message { get; set; }
}
Create a SmartPart user control

1. Add a new User Control named MyView.cs

2. Drag a Label control

3. Derive the class Myview from IMyView
public partial class MyView : UserControl, IMyView
4. Right-click IMyView and click Implement Interface.

5. Replace the two throw statements
public string Message
{
get
{
return this.label1.Text;
}
set
{
this.label1.Text = value;
}
}
Create a Presenter

6. Create a new class named MyPresenter.cs and add a variable of type IMyView
public class MyPresenter
{
IMyView view;
7. Create a constructor for the class. The constructor takes a reference to the view, sets it as the view for this presenter, and subscribes to the Load event of the view
public MyPresenter(IMyView view)
{
this.view = view;
view.Load += new EventHandler(view_Load);
}
8. Create an Event handler for the Load event. This sets the Message property of the Label control
void view_Load(object sender, EventArgs e)
{
view.Message = "Hello World from a Module";
}
Reference to the WorkItem

1. Open the MyModuleInit.cs and add a variable parentWorkItem. It reference to the root ShellWorkItem
private WorkItem parentWorkItem;

2. Add a public property to the class. It references the existing WorkItem, and passes back a reference to it.
[ServiceDependency]
public WorkItem ParentWorkItem
{
set { parentWorkItem = value; }
}
3. Modify the Load method
public override void Load()
{
base.Load();
MyWorkItem myWorkItem = parentWorkItem.WorkItems.AddNew<MyWorkItem>();
myWorkItem.Run(parentWorkItem.Workspaces["tabWorkspace1"]);
}

Create and show the view

4. Open the file MyWorkItem.cs
using Microsoft.Practices.CompositeUI.SmartParts;
5. Create a public Run method that accepts as a parameter a reference to the TabWorkspace (IWorkspace will let you change the type of workspace in future)
public void Run(IWorkspace TabWorkspace)
{
}
6. Add statements to the Run method that create a new instance of the MyView class and a new instance of the MyPresenter class. Passing the view instance you just created to the presenter connects them together
IMyView view = this.Items.AddNew<MyView>();
MyPresenter presenter = new MyPresenter(view);
7. Add statements to the Run method that add the new presenter to the current WorkItem and call the Show method of the TabWorkspace to display the view.
this.Items.Add(presenter);
TabWorkspace.Show(view);

No comments: