2014-05-11 28 views
7

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

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 
{ 
    /// <summary> 
    /// Initializes a new instance of the ViewModelLocator class. 
    /// </summary> 
    public ViewModelLocator() 
    { 
     if (!ViewModelBase.IsInDesignModeStatic) 
     { 
      ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

      if (ViewModelBase.IsInDesignModeStatic) 
      { 
       // Create design time view services and models 
       //SimpleIoc.Default.Register<IDataService, DesignDataService>(); 
      } 
      else 
      { 
       // Create run time view services and models 
       //SimpleIoc.Default.Register<IDataService, DataService>(); 
      } 

      SimpleIoc.Default.Register<HomePageViewModel>(); 
      SimpleIoc.Default.Register<FixturesViewModel>(); 
     } 
    } 

    [SuppressMessage("Microsoft.Performance", 
     "CA1822:MarkMembersAsStatic", 
     Justification = "This non-static member is needed for data binding purposes.")] 
    public HomePageViewModel Main 
    { 
     get 
     { 
      //return ServiceLocator.Current.GetInstance<HomePageViewModel>(); 
      return SimpleIoc.Default.GetInstance<HomePageViewModel>("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<FixturesViewModel>(); 
      return SimpleIoc.Default.GetInstance<FixturesViewModel>("default"); 
     } 
    } 

    public static void Cleanup() 
    { 
     // TODO Clear the ViewModels 
    } 
} 

我的观点XAML有这个

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

和我的应用程序有这个

<viewModel:ViewModelLocator x:Key="Locator" 
         d:IsDataSource="True"/> 

任何想法我做错了吗?

回答

12

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

SimpleIoc.Default.Register<HomePageViewModel>(); 

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

+0

即使注册错误仍然存​​在。 –

+0

同样在这里,但我有另外几十个其他ViewModel注册相同的方式。不明白为什么这个人会抛出这个错误。奇怪的是,它是一个空的,并且与其他的继承自同一个抽象类。困惑。还有其他建议吗? – Thierry

+0

当你说在设计模式下没有执行一些代码时,你不是说你是如何修复它的?你可以解释吗? – Thierry