C# PRISM:使用MVVM,如何解析或注入构造函数对象?

C# PRISM:使用MVVM,如何解析或注入构造函数对象?,c#,mvvm,prism,unity-container,ioc-container,C#,Mvvm,Prism,Unity Container,Ioc Container,我正在使用MVVM和PRISM。在这个项目中,我有一个名为IFoo的公共接口,其他模块应该实现这个接口并注册它 // Common module public interface IFoo { } // Module1 module public class Foo1 : IFoo { } 然后,当我初始化模块1时,我注册我的类型并导航 _container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManag

我正在使用MVVM和PRISM。在这个项目中,我有一个名为IFoo的公共接口,其他模块应该实现这个接口并注册它

// Common module
public interface IFoo { }

// Module1 module
public class Foo1 : IFoo { }
然后,当我初始化模块1时,我注册我的类型并导航

_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
_container.RegisterType<Object, View1>("View1");

var module = new Uri("View1", UriKind.Relative);
_regionManager.RequestNavigate("MainRegion", module);
在这之前,一切都好。但是稍后,我需要从外部模块获取Foo1。因此,我将另一个注册表设置为Foo1的映射名称:

_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
_container.RegisterType<IFoo, Foo1>("foo1", new ContainerControlledLifetimeManager());
\u container.RegisterType(新containerControlled LifetimeManager());
_RegisterType(“foo1”,新的ContainerControlledLifetimeManager());
没错,这对我来说很有效,但我不喜欢将两个实例分开的想法。我只需要一个,根据同一个实例

有没有办法解决这个问题? 提前谢谢

无论如何,我附加了一个Zip,其中包含一个代表我的问题的演示。

加载模块时,您可以在引导程序中注册所有类型

// register all modules
protected override void ConfigureModuleCatalog()
{
    // get all module types
    var types = new List<Type>();
    types.Add(typeof(ModuleA));
    types.Add(typeof(ModuleB));
    types.Add(typeof(ModuleC));

    // register all types
    foreach (var type in types)
    {
        ModuleCatalog.AddModule(new ModuleInfo()
        {
            ModuleName = type.Name,
            ModuleType = type.AssemblyQualifiedName
        });
    }
}

加载模块时,可以在引导程序中注册所有类型

// register all modules
protected override void ConfigureModuleCatalog()
{
    // get all module types
    var types = new List<Type>();
    types.Add(typeof(ModuleA));
    types.Add(typeof(ModuleB));
    types.Add(typeof(ModuleC));

    // register all types
    foreach (var type in types)
    {
        ModuleCatalog.AddModule(new ModuleInfo()
        {
            ModuleName = type.Name,
            ModuleType = type.AssemblyQualifiedName
        });
    }
}

我认为你不需要注册Foo1两次。您使用的是
ContainerControlledLifetimeManager
,因此,每当您向Unity容器请求IFoo实例时,它都会给出Foo1—您不需要使用名称作为键

因此,在
模块1中注册Foo1:

_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
System.Diagnostics.Debug.Print(Foo1.GetHashCode());
\u container.RegisterType(新containerControlled LifetimeManager());
System.Diagnostics.Debug.Print(Foo1.GetHashCode());
在您的外部模块中:

IFoo someFoo = _container.Resolve<IFoo>();

// someFoo is the same object as Foo1, so the hashcodes will be equal.
System.Diagnostics.Debug.Print(someFoo.GetHashCode());
ifoosomefoo=_container.Resolve();
//someFoo与Foo1是同一个对象,因此hashcode是相等的。
System.Diagnostics.Debug.Print(someFoo.GetHashCode());

我认为您不需要注册Foo1两次。您使用的是
ContainerControlledLifetimeManager
,因此,每当您向Unity容器请求IFoo实例时,它都会给出Foo1—您不需要使用名称作为键

因此,在
模块1中注册Foo1:

_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
System.Diagnostics.Debug.Print(Foo1.GetHashCode());
\u container.RegisterType(新containerControlled LifetimeManager());
System.Diagnostics.Debug.Print(Foo1.GetHashCode());
在您的外部模块中:

IFoo someFoo = _container.Resolve<IFoo>();

// someFoo is the same object as Foo1, so the hashcodes will be equal.
System.Diagnostics.Debug.Print(someFoo.GetHashCode());
ifoosomefoo=_container.Resolve();
//someFoo与Foo1是同一个对象,因此hashcode是相等的。
System.Diagnostics.Debug.Print(someFoo.GetHashCode());