Wpf 加载带有MEF和ExportAttribute的视图时出现问题

Wpf 加载带有MEF和ExportAttribute的视图时出现问题,wpf,mef,Wpf,Mef,我有一个WPF应用程序,我正在尝试使用MEF加载viewmodels和view。 我无法成功加载视图。 守则: public interface IContent { void OnNavigatedFrom( ); void OnNavigatedTo( ); } public interface IContentMetadata { string ViewUri { get; } } [MetadataAttribute] public class Exte

我有一个WPF应用程序,我正在尝试使用MEF加载viewmodels和view。 我无法成功加载视图。 守则:

public interface IContent
{

    void OnNavigatedFrom( );

    void OnNavigatedTo( );

}

public interface IContentMetadata
{
    string ViewUri { get; }
}

[MetadataAttribute]
public class ExtensionMetadataAttribute : ExportAttribute
{
    public string ViewUri { get; private set; }

    public ExtensionMetadataAttribute(string uri) : base(typeof(IContentMetadata))
    {
        this.ViewUri = uri;
    }
}

class ViewContentLoader 
{


    [ImportMany]
    public IEnumerable<ExportFactory<IContent, IContentMetadata>> ViewExports
    {
        get;
        set;
    }


    public object GetView(string uri)
    {
        // Get the factory for the View. 
        var viewMapping = ViewExports.FirstOrDefault(o =>
         o.Metadata.ViewUri == uri);

        if (viewMapping == null)
            throw new InvalidOperationException(
             String.Format("Unable to navigate to: {0}. " +
                "Could not locate the View.",
                uri));

        var viewFactory = viewMapping.CreateExport();
        var view = viewFactory.Value;
        return viewFactory;

    }
}
2) 由以下部分组成:

var cv = new CompositionContainer(aggregateCatalog);
var mef = new ViewContentLoader();
cv.ComposeParts(mef);
3) 在给定URI的运行时加载视图,例如:

private void CustomPause_Click(object sender, RoutedEventArgs e)
    {
        var vc = GlobalContainer.Instance.GetMefContainer() as ViewContentLoader;

        MainWindow.MainFrame.Content = vc.GetView ("CustomPause");
    }
问题是GetView方法中的此行失败:

    var viewMapping = ViewExports.FirstOrDefault(o =>
     o.Metadata.ViewUri == uri);
查询失败,因此viewMapping为null,但合成似乎正常,我可以看到ViewExports包含以下类型的对象:

{System.ComponentModel.Composition.ExportFactory<EyesGuard.MEF.IContent, EyesGuard.MEF.IContentMetadata>[0]
{System.ComponentModel.Composition.ExportFactory[0]
我不知道我错在哪里。你有线索吗

吉安波洛

我忘了这个

 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
在MetadataAttribute中

 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]