|
|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS Windows Forms Controls
Dependency Injection and Microsoft Windows Forms
A walkthrough using Microsoft's Composite UI Application Block
By: Guy Starbuck
Dec. 20, 2005 04:15 PM
Digg This!
Page 2 of 2
« previous page
Create the Second Module
Inject the Modules into the Shell First, modify the project properties for both module projects so that the output directory is the shell's output directory. This property can be found in the properties page for each module on the "Build" tab, toward the bottom. Modify the output directory for the assembly to be: ..\DataViewerShell\bin\Debug\ Next, create a new XML configuration file in the Shell project to specify the module. The XML file should be named "ProfileCatalog.xml," and is a listing of the available "profiles" for the shell application. Each profile is a distinct set of controls that are plugged into the shell. The format of the XML can be seen in Listing 4. Be sure to set the "Copy to Output Directory" property of the XML file in Visual Studio to "copy if newer" or "copy always." Now you should be able to build and run the solution, and the two list boxes should display in their appropriate positions. Stepping through the process of loading the application, it works like this:
Load the Browser List Box For this demo, we will be loading the XML in the Load event for the ListBox. Open the ListOrganizerListBox user control, and add a new Load event handler. Load up the XML file into a new untyped DataSet and bind it to the control as shown in Listing 6. Build and run the application - it should now show the items of type "List" in the "Lists" node in the module on the left side of the shell (see Figure 3).
Link the Modules Using Publish/Subscribe To publish the OnClick event in the ListOrganizerListBox, we need to do two things. First, add a new System.EventHandler<TEventArgs> delegate for subscribing event handlers. For the TEventArgs type, specify Microsoft.Practices.CompositeUI.Utility.DataEventArgs<string>, which we will use to hold the string value of the item selected in the list box. Mark this delegate with the Microsoft.Practices.CompositeUI.EventBroker.EventPublicationAttribute and pass in a descriptive name for the published event. The delegate declaration should look something like this:
[EventPublication("topic://ListOrganizer/ListSelected")] Now, add a normal event handler to the ListBox Click event. Pass the selected list to the delegate as a parameter. The complete code for the delegate and the Click event handler is shown in Listing 7. Finally we move to ListViewerList-Box, where we need to subscribe to the ListSelected event publication. Open the code for the ListViewerListBox control and add a new event handler method called OnListSelected, with parameters object sender and DataEventArgs<string> args. This method signature will work with the delegate method to which it is subscribing. Mark the method with the Microsoft.Practices.CompositeUI.EventBroker.EventSubscriptionAttribute, passing in the same descriptive name as you used in the EventPublication attribute. In the body of the method, load up a DataSet from the XML, filter the results based on the parameter coming in from the event, and bind to the resulting nodes. Obviously the loading of the data should be done more efficiently in a production application; this demo is simplified for clarity. Compile and run the application. It should now look like Figure 4 and work as designed, with selection of an item in the left ListBox causing the right ListBox to display its contents.
Conclusion Dependency Injection comes with a learning curve, but it is a pattern that is gaining popularity. As CAB has proven, Dependency Injection is very effective in a Windows Forms application framework. It provides true reusability of all components of a Windows Forms application. Resources
Page 2 of 2 « previous page
MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
||||||||||||||||||||||||||||||||||||||||