15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

exports ❘ 757<br />

So far, this control is just a simple WPF user control. To create a MEF part, the class<br />

TemperatureCalculatorExtension is exported by using the Export attribute. The class implements<br />

the interface ICalculatorExtension, offering Title <strong>and</strong> Description information, <strong>and</strong> returning the<br />

UserControl TemperatureConversion from the GetUI() method.<br />

using System.ComponentModel.Composition;<br />

using System.Windows;<br />

namespace Wrox.ProCSharp.MEF<br />

{<br />

[Export(typeof(ICalculatorExtension))]<br />

public class TemperatureCalculatorExtension : ICalculatorExtension<br />

{<br />

public string Title<br />

{<br />

get { return "Temperature Conversion"; }<br />

}<br />

public string Description<br />

{<br />

get { return "Convert Celsius to Fahrenheit <strong>and</strong> " +<br />

"Fahrenheit to Celsius"; }<br />

}<br />

}<br />

}<br />

public FrameworkElement GetUI()<br />

{<br />

return new TemperatureConversion();<br />

}<br />

code snippet TemperatureConversion/TemperatureCalculatorExtension.cs<br />

Another user control that implements the interface ICalculatorExtension is FuelEconomy. With this<br />

control either miles per gallon or liters per 100 km can be calculated.<br />

using System.Collections.Generic;<br />

using System.Windows;<br />

using System.Windows.Controls;<br />

namespace Wrox.ProCSharp.MEF<br />

{<br />

public partial class FuelEconomyUC : UserControl<br />

{<br />

private List fuelEcoTypes;<br />

public FuelEconomyUC()<br />

{<br />

InitializeComponent();<br />

InitializeFuelEcoTypes();<br />

}<br />

private void InitializeFuelEcoTypes()<br />

{<br />

var t1 = new FuelEconomyType<br />

{<br />

Id = "lpk",<br />

Text = "L/100 km",<br />

DistanceText = "Distance (kilometers)",<br />

FuelText = "Fuel used (liters)"<br />

};<br />

var t2 = new FuelEconomyType<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!