开发者

MEF example in Silverlight 4

Although there are many examples of Silverlight projec开发者_如何学运维ts using MEF (Managed Extensibility Framework), since the System.ComponentModel.Composition.Packaging.Toolkit package was removed in the version that is shipped inside Silverlight 4, these projects are away from helping to run some basic MEF example.

Some tutorials using the newer API will be very beneficial.

Thanks.


Although I can't point you in the direction of a concrete example, it's quite trivial to start composing parts of your Silverlight 4 application. Given the example:

public partial class MainPage : UserControl, IContext
{
  [ImportMany]
  public IEnumerable<IPlugin> Plugins { get; set; }

  public MainPage()
  {
    InitializeComponent();
    CompositionInitializer.SatisfyImports(this);

    Plugins.First().Run(this);
  }

  public void ShowMessage(string message)
  {
    textBox1.Text = message;
  }
}

public interface IContext
{
  void ShowMessage(string message);
}

public interface IPlugin
{
  void Run(IContext context);
}

[Export(typeof(IPlugin))]
public class SamplePlugin : IPlugin
{
  public void Run(IContext context)
  {
    context.ShowMessage("Hello World");
  }
}

The CompositionInitializer type provides SatisfyImports methods which action a default CompositionContainer which is plugged into a catalog that reads parts from your deployed XAP files. If you want more fine grained control over how the catalog is created, you can always create your own CompositionContainer.

Are there any particular aspects of MEF with Silverlight you are looking for advice on?


I wrote a blog post how you can implement MEF into you Silverlight applictaion see http://www.arrangeactassert.com/solid-design-principles-using-mef-in-silverlight-and-wpf/


I think this is what you are after.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜