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
- create a class inherit from Microsoft.Practices.CompositeUI.WorkItem.
- 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)steps2: In the child WorkItem, use the State attribute to indicate that a parent WorkItem should inject the property into the child WorkItem.
{
// set state for injection into child WorkItem
State["Customer"] = custmr;
ChildWorkItem myChild = this.WorkItems.AddNew<ChildWorkItem>();
myChild.Run();
}
// 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")]associate a Command with a UIElement
public void ShowName(object sender, EventArgs e)
{
MessageBox.Show("My name is Joe");
}
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))]Services that do not provide different implementations may not implement an interface
public class MyService : IMyService
{
}
[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
- create a new class library or Windows control library project
- add reference to Microsoft.Practices.CompositeUI and Microsoft.Practices.ObjectBuilder
- add a module attribute to identify this as a module.
- [assembly: Microsoft.Practices.CompositeUI.Module("mainMod")] in AssemblyInfo.cs
Create a module Initializer
- create a new public class
- inherit from Microsoft.Practices.CompositeUI.ModuleInit class.
- you can override the AddServices method
- you can override the Load method
put following in either in the ModuleInit or AssemblyInfo.cs
[assembly: Microsoft.Practices.CompositeUI.ModuleDependency(load a module
"TheModuleYouDependOn")]
<?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));declaratively
// or using generics
IMyService myServ = myWorkItem.Services.Get<IMyService>();
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]display a SmartPart in a Workspace
public partial class MySmartPart : UserControl
public class MyWorkItem : WorkItemimplement the MVC pattern
{
protected override void OnRunStarted()
{
base.OnRunStarted();
CustomerSmartPart csp = this.SmartParts.AddNew<CustomerSmartPart>();
Workspaces["tabbedArea"].Show(csp);
}
}
protected override void OnRunStarted()Publishing Events
{
base.OnRunStarted();
SampleView view = this.Items.AddNew<SampleView>();
workspace.Show(view);
}
[EventPublication("event://UpdatesAvailable", PublicationScope.Global)]Subscribing to Events
public event SomeEventHandler UpdatesAvailable;
[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:
Post a Comment