开发者

Found Possible Solution via SatisfyImports - Is There a Better Way?

Is there a better way to hook up dependencies?? This relies on the singleton App.Current and exposing a function that exposes the _container.SatisfyImports.

Is there a more MEF-tastic way of doing things?

Here is part of my main application class

public partial class App : Application
{
    private CompositionContainer _container;    

    ....

    public void SatisfyImportsOnce(Obje开发者_如何学Cct satifyMe)
    {
        _container.SatisfyImportsOnce(satisfyMe);    
    }
}

Here is a test class instantiated long after ComposeParts is called...

public class TestClass
{
    public TestClass()
    {
       Console.WriteLine("Created a TestClass");

       ((Microsoft.Samples.XFileExplorer.App)App.Current).SatisfyImportsOnce(this);
    }
}


I am in a similar situation in a WPF application where I want the MainWindow instance to import MEF exports. Since MEF does not create the MainWindow instance, it will not satisfy the imports unless you tell it to.

The way you are doing it will work if you do not want your instance to be registered for recomposition. If you do want recomposition, you should call ComposeParts.

Recomposition will update the imports in your class if and when they change.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜