Preparation
- Create a black solution, i.e. HelloWorld
- 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.
- In Visual Studio, create a new solution folder called “CompositeUI Source”. You may call it whatever you want.
- Add CAB projects under this folder.
The Shell Application
- Add a new project under the solution. Here we call it “ShellApplication”. It is a windows application
- Add reference to CAB
- Rename Form1.cs to MyShellForm.cs (*Note: you can name it whatever you want.)
- 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
- 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.
- 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
- 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.
- Rename Program.cs to MyShellApplication.cs (*Note: you can name it whatever you want.)
- Open MyShellApplication.cs
- Add some using statements
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.WinForms;
- 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();
}
}
}
- 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”
The Module
- 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
- Add reference to CAB
- 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.
- Create a new user control and name it “MyView”. We will put our “Hello World!” label here.
- 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);
}
}
}
- 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