Silverlight 4 中的 MEF 示例

发布于 2024-10-09 17:14:31 字数 198 浏览 0 评论 0原文

尽管有许多使用 MEF(托管扩展性框架)的 Silverlight 项目示例,但由于 System.ComponentModel.Composition.Packaging.Toolkit 包在 Silverlight 4 内附带的版本中被删除,因此这些项目无法帮助运行一些基本功能。 MEF 示例。

一些使用较新 API 的教程将非常有益。

谢谢。

Although there are many examples of Silverlight projects 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窗影残 2024-10-16 17:14:31

虽然我无法为您指明具体示例,但开始编写 Silverlight 4 应用程序的各个部分是非常简单的。给出示例:

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");
  }
}

CompositionInitializer 类型提供 SatisfyImports 方法,该方法操作默认的 CompositionContainer,该默认 CompositionContainer 插入到从部署的 XAP 文件中读取部分的目录中。如果您希望对目录的创建方式进行更细粒度的控制,您始终可以创建自己的CompositionContainer

您正在寻求关于 MEF 与 Silverlight 的任何特定方面的建议吗?

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?

江城子 2024-10-16 17:14:31

我写了一篇博客文章如何在 Silverlight 应用程序中实现 MEF 请参阅
http://www.arrangeactassert.com/实体设计原则-使用-mef-in-silverlight-and-wpf/

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/

尹雨沫 2024-10-16 17:14:31

我认为 就是您所追求的。

I think this is what you are after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文