Thursday, October 26, 2006

How to use Composite UI to build a simple winform application?

 

Preparation

  1. Create a black solution, i.e. HelloWorld

clip_image002

  1. Open File Explorer; create a new folder for CAB source code under the solution folder. You may call it “CompositeUI Source” and copy the CAB source from \\Program Files\Microsoft Composite UI App Block\CSharp\Source to this folder.
  2. In Visual Studio, create a new solution folder called “CompositeUI Source”. You may call it whatever you want.
  3. Add CAB projects under this folder.

clip_image004

The Shell Application

  1. Add a new project under the solution. Here we call it “ShellApplication”. It is a windows application

clip_image006

  1. Add reference to CAB

clip_image008

  1. Rename Form1.cs to MyShellForm.cs (*Note: you can name it whatever you want.)
  2. Open MyShellForm in design mode. We will need to add a work space to it. In this example, we will only add one main work space. If you don’t see any work space in you toolbox, you will need to add them from the CAB.

a.       Right click on “ToolBox” panel, Click on “Choose Items…” from the pop-up menu

 clip_image010

    1. Click on “Browse”, then find the Microsoft.Practices.CompositeUI.WinForms.dll in your CAB source directory. If you don’t find it, build your solution first.
    2. You will see the workspaces are highlighted in the list. Click on “OK” to close the window. The workspaces should be added to your toolbox
    3. We choose DeckWorkspace here and add it to the form. Change the “Dock” property to “Fill” and name it “mainWorkspace”. The form will look like below.

clip_image012

  1. Rename Program.cs to MyShellApplication.cs (*Note: you can name it whatever you want.)
  2. Open MyShellApplication.cs
  1. Add some using statements
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.WinForms;



  1. Make the following code change

namespace ShellApplication
{
class MyShellApplication : FormShellApplication<WorkItem, MyShellForm>
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
new MyShellApplication().Run();
}
}

}



  1. Add ProfileCatalog.xml to the ShellApplication project and have the following code added to it. You must name it ProfileCatalog.xml, otherwise CAB won’t be able to recognize it. This file is for loading your modules on the run time. The AssemblyFile attribute is your compiled module name which we will create later.

<?xml version="1.0" encoding="utf-8" ?>
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile" >
<Modules>
<ModuleInfo AssemblyFile="HelloWorldModule.dll"/>
</Modules>
</SolutionProfile>


            Change the “Copy to Output Directory” property to “Copy always”


clip_image014


The Module



  1. Create a new project under the solution. We call it “HelloWorldModule”. It is a Class Library. You can name it whatever you want, but it has to match what you put in the ProfileCatalog.xml

clip_image016



  1. Add reference to CAB

clip_image018



  1. Right click on the project and open “Properties” page. Click on “Build” from the left. Change the “Output Path” to “..\ShellApplication\bin\Debug\” which is your application run-time directory. This step is required, otherwise you will receive “HelloWorldModule.dll” was not found” exception.

clip_image020



  1. Create a new user control and name it “MyView”. We will put our “Hello World!” label here.

clip_image022



  1. Add a new class called “MyWorkItem” and have the following code added to it.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;

namespace HelloWorldModule
{
public class MyWorkItem : WorkItem
{
public void Show(IWorkspace workSpace)
{
MyView myView = this.Items.AddNew<MyView>();
workSpace.Show(myView);
}
}
}



  1. Add another new class called “MyModuleInit” and add the following code to it. Note that the “mainWorkspace” metioned below is the name of the work space in our shell form.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.CompositeUI;

namespace HelloWorldModule
{
public class MyModuleInit : ModuleInit
{
private WorkItem rootWorkItem;

[ServiceDependency]
public WorkItem RootWorkItem
{
set { rootWorkItem = value; }
}

public override void Load()
{
MyWorkItem myWorkItem = rootWorkItem.WorkItems.AddNew<MyWorkItem>();
myWorkItem.Show(rootWorkItem.Workspaces["mainWorkspace"]);
}
}
}


The End


Now you are ready to run the application. You need to set the startup application before running, though. Good luck!

No comments:

Post a Comment