C# 在缓存中找不到类型-UWP Windows 10

C# 在缓存中找不到类型-UWP Windows 10,c#,mvvm,win-universal-app,mvvm-light,C#,Mvvm,Win Universal App,Mvvm Light,我最近刚刚在我的UWP项目中添加了一个新的视图和视图模型,该项目使用MVVMLight,在过去的几个小时里,我一直在尝试找出这个文件和新文件之间的区别,但看不到任何不同 这可能是一个重复的帖子,但我觉得它从未真正得到回复 无论如何,我已经在ViewModelLocator中声明了十几个或更多的ViewModel,它们都可以正常工作,除了我的新ViewModel,它的定义方式与其他ViewModelLocator完全相同: SimpleIoc.Default.Register<UnlockF

我最近刚刚在我的UWP项目中添加了一个新的视图和视图模型,该项目使用MVVMLight,在过去的几个小时里,我一直在尝试找出这个文件和新文件之间的区别,但看不到任何不同

这可能是一个重复的帖子,但我觉得它从未真正得到回复

无论如何,我已经在ViewModelLocator中声明了十几个或更多的ViewModel,它们都可以正常工作,除了我的新ViewModel,它的定义方式与其他ViewModelLocator完全相同:

SimpleIoc.Default.Register<UnlockFeaturesPageViewModel>();
正如您所看到的,它继承自AbstractPageViewModel,该抽象类也非常简单:

public abstract class AbstractPageViewModel : ViewModelBase, IPageViewModel
{
    public AbstractPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice, CurrentPageEnum currentPage) 
    {
        this._telemetryService = telemetryService;
        this._dataService = dataservice;
        this._currentPage = currentPage;
    }
}
它还包含其他属性和方法,如果需要可以覆盖这些属性和方法。ViewModelBase是Galasoft.MVVMLight中的类,IPageViewModel是一个基本接口

当初始化组件()时发生错误;在CodeBehind中,调用并尝试初始化DataContext,其定义如下:

public class UnlockFeaturesPageViewModel : AbstractPageViewModel
{
    public UnlockFeaturesPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice, CurrentPageEnum currentPage) : 
           base(telemetryService, dataservice,
           CurrentPageEnum.UnlockFeatures)
}
<Page x:Class="MyApp.Views.UnlockFeaturesPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:MyApp.Views"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      DataContext="{Binding Source={StaticResource Locator},
                    Path=UnlockFeaturesViewModel}" >
当我执行代码时,它抛出以下错误:

'UnlockFeaturesPageViewModel' threw an exception of type 
'System.Reflection.TargetInvocationException'
TargetInvocationException was unhandled by user code

An exception of type 'System.Reflection.TargetInvocationException' 
occurred in mscorlib.ni.dll but was not handled in user code.
InnerException
包含以下详细信息:

Message: Type not found in cache: MyApp.Constants.CurrentPageEnum.
Source: GalaSoft.MvvmLight.Extras
StackTrace: at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type
            serviceType, String key, Boolean cache) at
            GalaSoft.MvvmLight.Ioc.SimpleIoc.MakeInstance[TClass]()
那么,为什么在缓存中找不到MyApp.Constants.CurrentPageEnum时会出现此错误呢。我已向CurrentPageEnum添加了一个新值,以匹配新添加的页面,但根据错误,该值已缓存在某个位置,并且未更新。我可能完全错了,但我想不出其他任何东西,因为代码与其他工作的ViewModels相同

它肯定与我的AbstractPageViewModel相关,就好像我的UnlockFeatureViewModel继承自ViewModelBase(来自Galasoft.MVVMLight),它不会抛出任何错误

至于为什么这可能是一篇重复的文章,是因为在另一篇类似的文章中,开发人员提到它只是没有在调试模式下执行下面的一行,但他没有提到他是如何修复它的

SimpleIoc.Default.Register<UnlockFeaturesPageViewModel>();
simpleoc.Default.Register();
我的问题是,它似乎是在调试模式下执行的,因为我正在单步执行它,并转到下一行,如前所述,只有当它试图将我的页面的DataContext设置为该特定视图模型时,才会发生错误

奇怪的是,
Release
模式下没有发生错误

你知道是什么原因造成的吗?我如何解决这个问题


谢谢。

我以前遇到过此错误,当ServiceLocator尝试将您的所有服务注入viewmodel时会产生此错误。 我不确定这是MVVM light的错误还是其他原因。我解决了这个问题,总是在viewmodels的构造函数中使用ServiceLocator来调用我的所有服务,而不修改我所有viewmodels的构造函数。 例如:

publicMyViewModel()
{
var myService=ServiceLocator.Current.GetInstance();
}

您的
解锁功能SpageViewModel
具有作为依赖项的枚举

public class UnlockFeaturesPageViewModel : AbstractPageViewModel
{
    public UnlockFeaturesPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice, CurrentPageEnum currentPage) :  // This one, CurrentPageEnum do not belong here, it's enum and can't be resolved
           base(telemetryService, dataservice,
           CurrentPageEnum.UnlockFeatures)
}
由于
CurrentPageEnum
不是可以实例化的类,因此解析失败

您应该像这样删除枚举

public class UnlockFeaturesPageViewModel : AbstractPageViewModel
{
    public UnlockFeaturesPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice) :
           base(telemetryService, dataservice,
           CurrentPageEnum.UnlockFeatures)
}

在创建对IoC容器的依赖关系时,不应在ViewModels中使用ServiceLocator。最重要的是,您的代码变得很难或不可能进行单元测试。。。。我昨天应该休息一下!!你太棒了!我不敢相信我说我所有的其他ViewModel都是相同的,而它们却不是!它们的定义如您在更正的答案中所述!非常感谢。
public class UnlockFeaturesPageViewModel : AbstractPageViewModel
{
    public UnlockFeaturesPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice, CurrentPageEnum currentPage) :  // This one, CurrentPageEnum do not belong here, it's enum and can't be resolved
           base(telemetryService, dataservice,
           CurrentPageEnum.UnlockFeatures)
}
public class UnlockFeaturesPageViewModel : AbstractPageViewModel
{
    public UnlockFeaturesPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice) :
           base(telemetryService, dataservice,
           CurrentPageEnum.UnlockFeatures)
}