我無法讓MEF滿足導入並可以使用一些幫助。使用.NET 4.5,Windows Store的MEF 2組合PCL
不幸的是,問題僅在生產代碼庫中體現出來。我試圖簡化這個問題,所以我可以發佈一個例子,看看我的理解力不足,但簡化版本的工作。所以這是要麼我錯過了差異(並且我已經盡了全力去仔細觀察這兩者),或者需要複製才能重現。我有一個WPF應用程序使用.NET 4.5類庫和可移植類庫(針對.NET 4.5和Windows 8商店應用程序)。我還沒有一個Windows 8商店的應用程序,但它是計劃(因此頭痛)。我使用MEF 2,我最近的NuGet的拉斷:
<package id="Microsoft.Composition" version="1.0.20" targetFramework="portable-net45+win" />
我猜我期待的是如何調試這一點,因爲我不能張貼實際的代碼的一些建議。我可以找到關於如何調試的大部分在線建議似乎都不適用於MEF 2,至少不是這種兼容PCL的版本。以下是我的簡化版本,但是,這個版本同樣適用。
namespace Portable.Contracts
{
public interface IExportable
{
string Name { get; }
}
}
namespace Desktop
{
[Export(typeof(IExportable))]
public class Exported : IExportable
{
public string Name
{
get { return "Exported"; }
}
}
}
namespace Portable
{
public class Importer
{
[Import]
public IExportable Exportable { get; set; }
public Importer()
{
MEFLoader.ResolveImports(this);
}
public string Name { get { return Exportable.Name; } }
}
}
namespace Portable
{
public class MEFLoader
{
private static CompositionHost Container { get; set; }
public static void SetContainer(CompositionHost container)
{
Container = container;
}
public static void ResolveImports(object target)
{
if(Container != null)
{
Container.SatisfyImports(target);
}
}
}
}
namespace WPFApp
{
public partial class App : Application
{
public App()
{
var container = new ContainerConfiguration()
.WithAssembly(typeof(Exported).Assembly)
.CreateContainer();
MEFLoader.SetContainer(container);
var importer = new Importer();
var importedName = importer.Name;
}
}
}
importedName確實得到值「Exported」。在我的生產代碼,我與細節CompositionFailedException:
其他資料: 「MainWindowViewModel」丟失的依賴「UserInformation」。