MVVM Light“在缓存中找不到类型”

我正在尝试将我的Windows Phone 8 Silverlight应用程序转换为8.1手机应用程序,作为通用应用程序的一部分。 我不知道这是否相关,因为这是我第一次尝试正确实现视图模型。 我想在Windows和Windows Phone中的视图之间共享数据。 无论如何,这是我得到的错误。

Error 3 Type not found in cache: ScoreAlerts.ViewModel.FixturesViewModel. C:\Users\Dave\Documents\Visual Studio 2012\Projects\Score Alerts\ScoreAlerts\ScoreAlerts.WindowsPhone\Pages\Fixtures.xaml 9 5 ScoreAlerts.WindowsPhone Error 4 Type not found in cache: ScoreAlerts.ViewModel.HomePageViewModel. C:\Users\Dave\Documents\Visual Studio 2012\Projects\Score Alerts\ScoreAlerts\ScoreAlerts.Shared\Pages\HomePage.xaml 34 9 ScoreAlerts.WindowsPhone 

这就是我的视图模型定位器的外观

 public class ViewModelLocator { ///  /// Initializes a new instance of the ViewModelLocator class. ///  public ViewModelLocator() { if (!ViewModelBase.IsInDesignModeStatic) { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); if (ViewModelBase.IsInDesignModeStatic) { // Create design time view services and models //SimpleIoc.Default.Register(); } else { // Create run time view services and models //SimpleIoc.Default.Register(); } SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); } } [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")] public HomePageViewModel Main { get { //return ServiceLocator.Current.GetInstance(); return SimpleIoc.Default.GetInstance("default"); } } [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")] public FixturesViewModel Fixtures { get { //return ServiceLocator.Current.GetInstance(); return SimpleIoc.Default.GetInstance("default"); } } public static void Cleanup() { // TODO Clear the ViewModels } } 

我认为XAML有这个

 DataContext="{Binding Fixtures, Source={StaticResource Locator}}" 

我的应用程序有这个

  

我有什么想法我做错了吗?

答案是一个相当简单的错误。 该位未在设计模式下执行

 SimpleIoc.Default.Register(); 

我的SimpleIoc.Default.Register()代码; 在一个从未在设计模式下执行的if语句中。

在我的例子中,目标类没有实现无参数构造函数。 该类包含的唯一构造函数接受了一个字节类型参数,所以我得到:

在缓存中找不到类型:System.Byte

我的注册行是这样的:

 SimpleIoc.Default.Register(); 

我向MyConcreteClass添加了一个无参数构造MyConcreteClass ,然后将[PreferredConstructor]属性应用于它(此属性在GalaSoft.MvvmLight.Ioc命名空间中可用)并解决了这个问题。

希望这可以帮助有人在路上。