English 中文(简体)
PRISM: Using MVVM, how to resolution orjection in a Constructionor Object?
原标题:PRISM: Using MVVM, how to resolve or inject in a constructor objects?

I m 采用MTV和PRISM。 在该项目中,I ve进入一个称为“红十字与红新月”的共同接口,其他模块应当实施这一接口并登记。

// 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);

观点1

    public ViewModel1(IFoo foo, IEventAggregator eventAggregator, IRegionManager regionManager)
    {
        ...
    }

直到此为止,情况就好了。 但后来,我需要从外部单元获得Foo1。 因此,我设立了另一个登记册,以绘制Foo:

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

也是正确的,它为我工作,但我不喜欢把两个例子分开。 我只需要一个例子,需要同样的例子。

Is there a way to fix this scenario? Thanks in advance.

Anyway, I attach a Zip where contains a demo which represents my problem. http://www.mediafire.com/?feod8x0b952457e

最佳回答

当你装上模块时,你可以把所有类型登记在trap。

// 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
        });
    }
}

随后在<代码>ConfigureContainer上,你描述了你想在晚些时候查阅的所有类型和/或情况。 组合的集装箱被装入模块1模块的构件。

// register all types in all modules
protected override void ConfigureContainer()
{
    base.ConfigureContainer();

    // ModuleA
    Container.RegisterType<IInterface1, Type1>();
    Container.RegisterType<IInterface2, Type2>();

    // ModuleB
    Container.RegisterInstance<IInterface3>("name", new Type3());
    Container.RegisterType<IInterface4, Type4>();

    // ModuleC
    Container.RegisterType<IInterface5, Type5>();
    Container.RegisterType<IInterface6, Type6>();
}
问题回答

我认为,你确实需要登记Foo1两次。 页: 1 如果你要求团结集装箱进行国际红十字与红新月联会,它将给你Foo1——你不需要用一个名字作为钥匙。

因此,在<代码>中 页: 1

_container.RegisterType<IFoo, Foo1>(new ContainerControlledLifetimeManager());
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());




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签