如何使用MVVMLight SimpleIoc?

我正在对软件进行升级,该软件有些混乱Messenger.Default(...)

是否有任何备忘单来了解MVVMLight SimpleIoc的用法(不是一般的IoC描述)?


潇潇雨雨
浏览 1141回答 2
2回答

一只萌萌小番薯

SimpleIoc婴儿床床单:1)您在ViewModelLocator中注册了所有接口和对象class ViewModelLocator&nbsp;{&nbsp;&nbsp; &nbsp; static ViewModelLocator()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (ViewModelBase.IsInDesignModeStatic)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SimpleIoc.Default.Register<IDataService, DataService>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; SimpleIoc.Default.Register<MainViewModel>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; SimpleIoc.Default.Register<SecondViewModel>();&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public MainViewModel Main&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ServiceLocator.Current.GetInstance<MainViewModel>();&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }}&nbsp;2)默认情况下,每个对象都是单例。要解析对象以使其不是单例,您需要将唯一值传递给GetInstance调用:SimpleIoc.Default.GetInstance<MainViewModel>(Guid.NewGuid().ToString());3)要针对接口注册类:SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();&nbsp;&nbsp;4)要针对接口注册具体对象:SimpleIoc.Default.Register<IDataService>(myObject);&nbsp; &nbsp; &nbsp;5)要注册具体类型:SimpleIoc.Default.Register<MainViewModel>();&nbsp; &nbsp;6)要从接口解析对象:SimpleIoc.Default.GetInstance<IDataService>();7)直接解析对象(建立和依赖关系解析):SimpleIoc.Default.GetInstance<MainViewModel>();8)MVVM使设计时数据的确非常容易:if (ViewModelBase.IsInDesignModeStatic)&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; SimpleIoc.Default.Register<IDataService, DataService>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}&nbsp;&nbsp;如果您处于设计时模式,它将自动注册您的设计时服务,这使得在VS设计器中工作时在视图模型和视图中存储数据变得非常容易。希望这可以帮助。

浮云间

1)我发现SimpleIoc抛出设计时异常,因为服务已经注册。如果我检查IsRegistered,然后在我的视图模型中引用SimpleIoc,则不会显示设计时数据(不会引发异常,只是不显示)。如果删除SimpleIoc并手动创建数据,那么它将在设计时显示出来。因此,不,我认为这“真的很简单” :)请注意,数据服务在设计时间之外工作良好。
打开App,查看更多内容
随时随地看视频慕课网APP