c# - 使用 WPF 应用程序运行多个使用 MEF 的各种应用程序

标签 c# wpf mef

我正在尝试创建一个 WPF 应用程序,它将成为其他应用程序的中心点。主应用程序应该能够根据用户需要动态加载其他应用程序。在做了一些研究之后,MEF 似乎可以解决这个问题。

我对 MEF 很陌生,所以我编写了一个测试应用程序并尝试让 MEF 工作。测试应用程序定义了一个非常基本的 ITool 接口(interface)。我能够毫无困难地从类库中导入多个类,但是我无法导入另一个 WPF 应用程序。 MEF 可以做到这一点吗?

我的主要 WPF 应用程序创建了一个 ToolContainer 实例,它创建并组合了部件。

class ToolContainer
{
    [ImportMany(typeof(ITool))]
    IEnumerable<Lazy<ITool>> _tools;

    private CompositionContainer _container;

    public ToolContainer()
    {
        AggregateCatalog catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new DirectoryCatalog(
            "C:\\Application Development\\Tool Center\\Tool Extensions"));

        _container = new CompositionContainer(catalog);

        try
        {
            _container.ComposeParts(this);
        }
        catch (CompositionException compositionException)
        {
            //TODO: show error dialog
        }
    }

}

这是界面

public interface ITool
{
    String ToolName { get; }
    void OpenTool();
}

我已经创建了多个继承自 ITool 的类,并且导出工作正常。一个例子在这里。

[Export(typeof(ITool))]
public class Class1 : ITool
{
    public String ToolName
    {
        get { return "....."; }
    }

    public void OpenTool()
    {
        //open the tool
    }
}

现在我想导入一个继承了 ITool 接口(interface)的整个 WPF 应用程序。这里的想法是外部用户可以创建他们自己的 WPF 应用程序,这些应用程序将能够从主 WPF 应用程序运行。将来 ITool 界面会更高级,但我现在正努力完成基础工作。我创建了一个新的测试 WPF 项目并将 App.xaml.cs 修改为以下内容:

[Export(typeof(ITool))]
public partial class App : Application, ITool
{
    public String ToolName
    {
        get { return "Tool Sample"; }
    }

    public void OpenTool()
    {
       //open the tool
    }
}

当我执行此操作时,_tools 不包含我的新 WPF 应用程序,但包含 Class1。任何想法我做错了什么?有更好的方法吗?任何帮助将不胜感激。

最佳答案

默认情况下,DirectoryCatalog's constructor仅在目录中的 *.dll 文件中搜索类型。您可以通过添加第二个目录以在 .NET 可执行文件中查找类型来使此“工作”,如下所示:

AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(
        "C:\\Application Development\\Tool Center\\Tool Extensions"));
catalog.Catalogs.Add(new DirectoryCatalog(
        "C:\\Application Development\\Tool Center\\Tool Extensions", "*.exe")); 

话虽如此,我不建议这样做。与其让您的用户导出应用程序,我建议让他们导出一个实现了 IToolUserControl。这将以更符合预期的方式运行,因为“应用程序”实际上是设计并预期作为其自己的进程运行,而不是在单个父进程中组成。

关于c# - 使用 WPF 应用程序运行多个使用 MEF 的各种应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4866203/

相关文章:

c# - 我可以限制哪些类可以实例化另一个类吗?

c# - WPF 中子元素的样式触发器

c# - 动态控制 C# 上的验证器噩梦

wpf - 如何在WPF中将宽度设置为100%

c# - 使用MEF加载插件时如何使用Hangfire?

c# - 如何在运行时删除 MEF 插件?

c# - 释放和更新基于 DHCP 的 IP 地址

wpf - 更改 ItemsSource 时 GridView 列宽不会更新

c# - 带有 MEF 的 ASP.NET MVC4 : Requests to MEF Controllers Processed Serially

c# - 从文本框 C# 获取行索引