如何在MvvmLight中的IoC中注册多接口实现? [英] How to Register Multiple Interface Implementation In IoC in MvvmLight?

查看:260
本文介绍了如何在MvvmLight中的IoC中注册多接口实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在LightInject IoC中注册多接口实现

如何使用MvvmLight的Ioc解决问题? 我有多个DataService(DataService1,DataService2,DataService3 ...).它们都是IDataService,需要与多个ViewModel联系. Mvvmlight无法做到:

How to use MvvmLight's Ioc to solve the problem? I have multiple DataService (DataService1, DataService2, DataService3 ...). They are all IDataService and need to be contacted with multiple ViewModel. Mvvmlight can't do it:

SimpleIoc.Default.Register<IDataService, DataService1>("DataService1Key");
SimpleIoc.Default.Register<IDataService, DataService2>("DataService2Key");
...

推荐答案

您也可以在MvvmLight中使用类"键标识符,就像这样

You can also use 'class' key identifiers in MvvmLight, like so

Class1 c1 = new Class1();
Class2 c2 = new Class2();

SimpleIoc.Default.Register<IDataClass>(() => c1, "Class1");
SimpleIoc.Default.Register<IDataClass>(() => c2, "Class2");

var t = SimpleIoc.Default.GetInstance<IDataClass>("Class1");
var s = SimpleIoc.Default.GetInstance<IDataClass>("Class2");

这篇关于如何在MvvmLight中的IoC中注册多接口实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆