<接口>使用MVVMLightV4在混合中已注册错误 [英] <Interface> is already registered error in blend using MVVMLightV4

查看:50
本文介绍了<接口>使用MVVMLightV4在混合中已注册错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MVVMLight已有一段时间了,自从V4首次发布以来就开始使用它.

但是我始终无法解决一个问题.

如果我使用MVVM 4项目模板创建项目,则该解决方案可以很好地构建,运行良好并且可以很好地工作.

但是

如果我启动自己的项目并使用Nuget安装MVVMLight V4,那么即使我添加了基本的DataService并在ViewModelLocator模板中启用注释掉的代码并在Blend中打开项目,我也只会收到错误消息:

班级已经注册"

只是试图探究这个非常烦人的错误的根源.

请注意,DataService中的内容并不重要,即使只有一个空函数的接口和服务也会产生相同的错误

解决方案

由于某种原因,混合可能会多次初始化ViewModelLocator.

所以您需要添加支票:

if (!SimpleIoc.Default.IsRegistered<ISomeService>())
    SimpleIoc.Default.Register<ISomeService, SomeService>();

I've been using MVVMLight for some time now and started using V4 since it was first made available.

However one issue has always persisted with me which I've been unable to solve.

If I create a project using the MVVM 4 project template the solution builds fine, runs fine and works ok in blend.

However

If I start my own project and install MVVMLight V4 using Nuget, as soon as I add even a basic DataService and enable the commented out code in the ViewModelLocator template and open up the project in Blend I just get an error:

"Class is already registered"

Just trying to get to the bottom of this very annoying error.

Just to be clear it doesn't matter what is in the DataService as even with an interface and service with only 1 empty function gives the same error

解决方案

Blend, for some reason, may initialize the ViewModelLocator more than once.

So you need to add a check:

if (!SimpleIoc.Default.IsRegistered<ISomeService>())
    SimpleIoc.Default.Register<ISomeService, SomeService>();

这篇关于&lt;接口&gt;使用MVVMLightV4在混合中已注册错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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