15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

766 ❘ ChaPTer 28 mAnAGed extensibility frAmewOrk<br />

to all available exports from the catalog. Because here multiple add-ins are dynamically loaded (the user<br />

can select the add-in to use), the Menu control is dynamically extended. The application iterates through all<br />

the calculator extensions, <strong>and</strong> a new MenuItem is created for each extension. The header of the MenuItem<br />

control contains a Label with the Title property of the extension <strong>and</strong> a CheckBox. The CheckBox will<br />

later be used to remove the export dynamically. The ToolTip property of the MenuItem is set to the<br />

Description property of the extender control. The Tag property is set to the extender control itself,<br />

which makes it easy to get to the extender control when the menu item is selected. The Click event of the<br />

MenuItem is set to the method ShowAddIn(). This method invokes the GetUI() method of the extender<br />

control for displaying the control within a new TabItem of a TabControl.<br />

private void RefreshExensions()<br />

{<br />

catalog.Refresh();<br />

calcExtensionImport = new CalculatorExtensionImport();<br />

calcExtensionImport.ImportsSatisfied += (sender, e) =><br />

{<br />

this.textStatus.Text += String.Format("{0}\n", e.StatusMessage);<br />

};<br />

container.ComposeParts(calcExtensionImport);<br />

menuAddins.Items.Clear();<br />

foreach (var extension in calcExtensionImport.CalculatorExtensions)<br />

{<br />

var menuItemHeader = new StackPanel { Orientation = Orientation.Horizontal };<br />

menuItemHeader.Children.Add(new Label { Content = extension.Title });<br />

var menuCheck = new CheckBox { IsChecked = true };<br />

menuItemHeader.Children.Add(menuCheck);<br />

}<br />

}<br />

var menuItem = new MenuItem {<br />

Header = menuItemHeader,<br />

ToolTip = extension.Description,<br />

Tag = extension<br />

};<br />

menuCheck.Tag = menuItem;<br />

menuItem.Click += ShowAddIn;<br />

menuAddins.Items.Add(menuItem);<br />

private void ShowAddIn(object sender, RoutedEventArgs e)<br />

{<br />

var mi = e.Source as MenuItem;<br />

var ext = mi.Tag as ICalculatorExtension;<br />

FrameworkElement uiControl = ext.GetUI();<br />

var headerPanel = new StackPanel { Orientation = Orientation.Horizontal };<br />

headerPanel.Children.Add(new Label { Content = ext.Title });<br />

var closeButton = new Button { Content = "X" };<br />

var ti = new TabItem { Header = headerPanel, Content = uiControl };<br />

closeButton.Click += delegate<br />

{<br />

tabExtensions.Items.Remove(ti);<br />

};<br />

headerPanel.Children.Add(closeButton);<br />

}<br />

tabExtensions.SelectedIndex = tabExtensions.Items.Add(ti);<br />

code snippet WPFCalculator/MainWindow.xaml.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!