Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 处理Caliburn Micro&x27;静态国际奥委会_C#_.net_Wpf_Prism_Caliburn.micro - Fatal编程技术网

C# 处理Caliburn Micro&x27;静态国际奥委会

C# 处理Caliburn Micro&x27;静态国际奥委会,c#,.net,wpf,prism,caliburn.micro,C#,.net,Wpf,Prism,Caliburn.micro,我需要在一个基于棱镜的容器中承载多个模块(全部使用Caliburn Micro完成)。在容器中,可以从同一模块创建多个视图(例如:可以从计算器模块创建科学和金融计算器) 我使用Unity for DI,因此在所有模块中覆盖了Caliburn Micro的引导程序,以从Unity容器中解析 由于类是一个静态类,最后一个注册其依赖项的模块将覆盖()-请参见IoC.GetInstance=GetInstance行 我非常喜欢Sniffer提出的想法,但每个模块都创建了自己的子容器,因此它不适用于我的场

我需要在一个基于棱镜的容器中承载多个模块(全部使用Caliburn Micro完成)。在容器中,可以从同一模块创建多个视图(例如:可以从计算器模块创建科学和金融计算器)

我使用Unity for DI,因此在所有模块中覆盖了Caliburn Micro的引导程序,以从Unity容器中解析

由于类是一个静态类,最后一个注册其依赖项的模块将覆盖()-请参见IoC.GetInstance=GetInstance行


我非常喜欢Sniffer提出的想法,但每个模块都创建了自己的子容器,因此它不适用于我的场景。

我将提出一个我认为可行的解决方案。默认情况下,CM在
BootstrapperBase
中分配
IoC.GetInstance()
和所有其他
Func
委托,如下所示:

IoC.GetInstance = this.GetInstance
public class CalculatorBootstrapper : BootstrapperBase {

    private Func<Type, string, object> _previousGet;     

    public override void Configure() {
        _previousGet = IoC.GetInstance; // store reference to whatever was stored previously
        IoC.GetInstance = this.GetInstance;
    }

    public override Object GetInstance(Type type, string key) {
        var result = null;
        if (_previousGet != null)
            result = _previousGet(type, key);
        if (result == null) {
            // Try to use the local container here

        }
        return result;
    }
}
其中,
this.GetInstance
只是
BootstrapperBase
中的一个方法,它是虚拟的、空的,因此您可以在自己的派生引导程序中重写它

我尝试的解决方案是:存储对已在
IoC.GetInstance
注册的内容的引用,并在新的
GetInstance
覆盖中调用它,然后对
IoC
的其他两个静态
Func
执行此操作

在引导程序的构造函数或
Configure()
方法中,为
IoC
中相互包装的静态
Func
委托提供挂钩,如下所示:

IoC.GetInstance = this.GetInstance
public class CalculatorBootstrapper : BootstrapperBase {

    private Func<Type, string, object> _previousGet;     

    public override void Configure() {
        _previousGet = IoC.GetInstance; // store reference to whatever was stored previously
        IoC.GetInstance = this.GetInstance;
    }

    public override Object GetInstance(Type type, string key) {
        var result = null;
        if (_previousGet != null)
            result = _previousGet(type, key);
        if (result == null) {
            // Try to use the local container here

        }
        return result;
    }
}
公共类计算器bootstrapper:bootstrapper数据库{
私有函数;
公共覆盖无效配置(){
_previousGet=IoC.GetInstance;//存储对以前存储的内容的引用
IoC.GetInstance=this.GetInstance;
}
公共重写对象GetInstance(类型,字符串键){
var结果=null;
if(_previousGet!=null)
结果=_previousGet(类型、键);
如果(结果==null){
//尝试使用本地容器
}
返回结果;
}
}

我将提出一个我认为可行的解决方案。默认情况下,CM在
BootstrapperBase
中分配
IoC.GetInstance()
和所有其他
Func
委托,如下所示:

IoC.GetInstance = this.GetInstance
public class CalculatorBootstrapper : BootstrapperBase {

    private Func<Type, string, object> _previousGet;     

    public override void Configure() {
        _previousGet = IoC.GetInstance; // store reference to whatever was stored previously
        IoC.GetInstance = this.GetInstance;
    }

    public override Object GetInstance(Type type, string key) {
        var result = null;
        if (_previousGet != null)
            result = _previousGet(type, key);
        if (result == null) {
            // Try to use the local container here

        }
        return result;
    }
}
其中,
this.GetInstance
只是
BootstrapperBase
中的一个方法,它是虚拟的、空的,因此您可以在自己的派生引导程序中重写它

我尝试的解决方案是:存储对已在
IoC.GetInstance
注册的内容的引用,并在新的
GetInstance
覆盖中调用它,然后对
IoC
的其他两个静态
Func
执行此操作

在引导程序的构造函数或
Configure()
方法中,为
IoC
中相互包装的静态
Func
委托提供挂钩,如下所示:

IoC.GetInstance = this.GetInstance
public class CalculatorBootstrapper : BootstrapperBase {

    private Func<Type, string, object> _previousGet;     

    public override void Configure() {
        _previousGet = IoC.GetInstance; // store reference to whatever was stored previously
        IoC.GetInstance = this.GetInstance;
    }

    public override Object GetInstance(Type type, string key) {
        var result = null;
        if (_previousGet != null)
            result = _previousGet(type, key);
        if (result == null) {
            // Try to use the local container here

        }
        return result;
    }
}
公共类计算器bootstrapper:bootstrapper数据库{
私有函数;
公共覆盖无效配置(){
_previousGet=IoC.GetInstance;//存储对以前存储的内容的引用
IoC.GetInstance=this.GetInstance;
}
公共重写对象GetInstance(类型,字符串键){
var结果=null;
if(_previousGet!=null)
结果=_previousGet(类型、键);
如果(结果==null){
//尝试使用本地容器
}
返回结果;
}
}

我看不出确切的问题,您正在尝试计算器模块的多个实例,因此所有
GetInstance()
函数注册应该是相同的,那么为什么不希望发生重写呢?我希望你能更清楚地解释你的问题。谢谢。你是对的,很抱歉我错过了这一部分,在我的场景中,容器将包含多个不同模块的实例(比如日历、计算器、记事本等),所有模块都使用CM。由于IoC是静态的,如果在最后加载记事本模块,那么它将覆盖日历和计算器注册。我看不出确切的问题,您正在尝试计算器模块的多个实例,因此所有
GetInstance()
函数注册都应相同,那么为什么不希望发生覆盖?我希望你能更清楚地解释你的问题。谢谢。你是对的,很抱歉我错过了这一部分,在我的场景中,容器将包含多个不同模块的实例(比如日历、计算器、记事本等),所有模块都使用CM。由于IoC是静态的,如果在最后加载记事本模块,那么它将覆盖日历和计算器注册。这有点黑客,但我喜欢:)@Charleh谢谢。我知道这是一个有点黑客,我希望它能起作用,因为你可以看到他的情况不是很常见,有点奇怪,所以解决办法也是这样:)。这个主意不错,谢谢。这将适用于大多数情况,我们可以迭代先前注册的容器链来解决问题,否则使用本地容器。但当模块创建子容器时,这将不起作用,例如宿主科学和金融计算器(同一模块),这两个计算器将具有从父容器派生的注册,但它们也可能具有私有注册(通过子容器示例IFormatNumber),在这种情况下,解析将不起作用。如果从第二个计算器启动,对IFormatNumber的查询将始终从上一个容器中解析。@RohitSharma您能否显示一些代码或概述,说明同一模块中的这两个计算器如何使用子容器?Hi Sniffer,下面是一个显示子容器如何初始化的代码段,实际上,每次我们创建一个视图时,lambda都会被调用(这是初始化子容器并传递给引导程序的地方),因为多个视图都会注册自己的INumberFormatProvider,即