如何通过使用Prism 4.0进行集成的Silverlight项目中的构造函数将参数从Shell传递到模块

时间:2011-07-18 13:06:16

标签: silverlight-4.0 prism-4

我有一个主要的Silverlight Shell项目,它调用了几个Silverlight模块项目。 我需要通过构造函数将参数传递给我的模块项目。

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

“问问,是否会接受”是国际奥委会的座右铭:)

Prism通过 UnityContainer 使用注入。加载模块时,它将解析模块构造函数中指定的任何已注册的接口

只需指定一个先前已注册为单例的对象的接口,它将与任何模块一起传递给它。将所有设置/参数放在该单例中。

如果您需要更多信息,请询问。

答案 1 :(得分:1)

使用容器注册对象。

class MyBootStrapper : UnityBootstrapper
{
    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        this.Container.RegisterInstance(typeof(IMyInterface), new MyInterfaceImpl());
    }
}

现在,模块构造函数很高兴收到该对象。

class ContentModule : IModule
{
    private readonly IMyInterface _myInterfaceImpl;

    public ContentModule(IMyInterface myInterfaceImpl)
    {
        _myInterfaceImpl = myInterfaceImpl;
    }

    #region IModule Members
    //
    #endregion
}

礼貌:TrueBlueAussie