Tuesday, May 29, 2007

Basic Commands...

Creating a shell.
public class MyApplication : FormShellApplication<WorkItem, MyShellForm>
{
}
[STAThread]
public static void Main()
{
new MyApplication.Run();
}

override the AfterShellCreated method
protected override void AfterShellCreated()
{
base.AfterShellCreated();
... your start-up code here ...
}

display a SmartPart in a Workspace
Form1 mainForm = new Form1();
CustomerSmartPart sp = myWorkItem.Items.AddNew<CustomerSmartPart>();
mainForm.deckWorkspace1.Show(sp);

WorkItems.

create a workitem
  1. create a class inherit from Microsoft.Practices.CompositeUI.WorkItem.
  2. override the OnRunStarted method. In this method, add code to perform an initialization required and to display the appropriate view.
protected override void OnRunStarted()
{
base.OnRunStarted();
SummaryView view = this.Items.AddNew<SummaryView>();
workspace.Show(view);
}

Invoke a WorkItem
myWorkItem.Run();


Inject state into child SmartParts in a child WorkItem

steps1: In the parent WorkItem, set the state so that adding a child WorkItem to the container injects the state into it
public void ShowCustomerDetails(Customer custmr)
{
// set state for injection into child WorkItem
State["Customer"] = custmr;
ChildWorkItem myChild = this.WorkItems.AddNew<ChildWorkItem>();
myChild.Run();
}
steps2: In the child WorkItem, use the State attribute to indicate that a parent WorkItem should inject the property into the child WorkItem.
// in child WorkItem
[State("Customer")]
public Customer TheCustomer
{
get { return (Customer)State["Customer"]; }
set { State["Customer"] = value; }
}

UIElements.
RootWorkItem.UIExtensionSites.RegisterSite("MainMenu", Shell.MainMenuStrip);
ToolStripMenuItem item = null;
item = new ToolStripMenuItem("Name");
MyWorkItem.UIExtensionSites["MainMenu"].Add(item);

Commands.
[CommandHandler("ShowName")]
public void ShowName(object sender, EventArgs e)
{
MessageBox.Show("My name is Joe");
}

associate a Command with a UIElement
MyWorkItem.Commands["ShowName"].AddInvoker(item, "Click");


Services.

add a service by specifying it in the shell application configuration file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CompositeUI"
type="Microsoft.Practices.CompositeUI.Configuration.SettingsSection,
Microsoft.Practices.CompositeUI"
allowExeDefinition="MachineToLocalUser" />
</configSections>
<CompositeUI>
<services>
<!-- Other services -->
<add serviceType="MyApp.Services.IMyService, MyApp"
instanceType="MyApp.Services.MyService, MyApp"/>
</services>
</CompositeUI>
</configuration>


to add a service programmatically

1. to use an existing service instance already created.
RootWorkItem.Services.Add<CustomerService>(myServiceInstance);

2. to create a new instance of a service
RootWorkItem.Services.AddNew<CustomerService>();


Register a class as a service using attribute.
[Service(typeof(IMyService))]
public class MyService : IMyService
{
}
Services that do not provide different implementations may not implement an interface
[Service]
public class MyService
{
}

declare a class to be registered as a lazy-loaded service.
[Service(typeof(IMyService), AddOnDemand=true)]
public class MyService : IMyService
{
}

Module.

steps1 : Create a module
steps2 : Create a module initializer
steps3 : add dependencies to the module
steps4 : load a module

Create a module
  1. create a new class library or Windows control library project
  2. add reference to Microsoft.Practices.CompositeUI and Microsoft.Practices.ObjectBuilder
  3. add a module attribute to identify this as a module.
  4. [assembly: Microsoft.Practices.CompositeUI.Module("mainMod")] in AssemblyInfo.cs

Create a module Initializer
  1. create a new public class
  2. inherit from Microsoft.Practices.CompositeUI.ModuleInit class.
  3. you can override the AddServices method
  4. you can override the Load method
add dependencies
put following in either in the ModuleInit or AssemblyInfo.cs
[assembly: Microsoft.Practices.CompositeUI.ModuleDependency(
"TheModuleYouDependOn")]
load a module
<?xml version="1.0" encoding="utf-8" ?>
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile">
<Modules>
<ModuleInfo AssemblyFile="Mod1.dll"/>
<ModuleInfo AssemblyFile="Mod2.dll"/>
</Modules>
</SolutionProfile>

creating reference to services

programmatically
IMyService myServ = (IMyService)myWorkItem.Services.Get(typeof(IMyService));

// or using generics
IMyService myServ = myWorkItem.Services.Get<IMyService>();
declaratively
private IMyService service;

[ServiceDependency]
public IMyService MyService
{
set { service = value; }
}


creating Custom Services
steps1: add a new interface to an appropriate module.
steps2: add members that the interface will define.
steps3: add a new class to the module or the shell.
steps4: inherit from the interface and add required functionality to the members.
steps5: add the attribute like following
[Service(Type=typeof(IMyService))]
public class MyService : IMyService

SmartParts.

steps1: add a user control to project
steps2: add a reference to the Microsoft.Practices.CompositeUI.SmartParts
steps3: add the attribute as following
[SmartPart]
public partial class MySmartPart : UserControl

display a SmartPart in a Workspace
public class MyWorkItem : WorkItem
{
protected override void OnRunStarted()
{
base.OnRunStarted();
CustomerSmartPart csp = this.SmartParts.AddNew<CustomerSmartPart>();
Workspaces["tabbedArea"].Show(csp);
}
}
implement the MVC pattern
protected override void OnRunStarted()
{
base.OnRunStarted();
SampleView view = this.Items.AddNew<SampleView>();
workspace.Show(view);
}
Publishing Events
[EventPublication("event://UpdatesAvailable", PublicationScope.Global)]
public event SomeEventHandler UpdatesAvailable;

Subscribing to Events
[EventSubscription("event://UpdatesAvailable")]
public void NewUpdates(object sender, SomeEventArgs numUpdates)
{
MessageBox.Show(numUpdates.ToString(), "Updates available");
}

run an event on a background thread
[EventSubscription("event://UpdatesAvailable",
Thread=ThreadOption.Background)]

No comments: