2014-12-30 41 views
1

我试图把一个通用的应用程序一起,我使用MVVM轻,但编译我的应用程序时,我收到以下错误:类型在缓存中没有发现

Error 1 Type not found in cache: MyApp.Model.LocationModel 
...\MyApp.WindowsPhone\Views\LocationPage.xaml 10 5 MyApp.WindowsPhone 

它编译成功,但我无法弄清楚是什么导致了这个问题。我发现一对夫妇的文章对计算器:

SimpleIoC - Type not found in cache: Windows.UI.Xaml.Controls.Frame

MVVM Light “Type Not Found in cache”

但没有一个适用于我的问题。我注意到的第一件事是,该错误以某种方式显示问题所在的模型而不是ViewModel。

Error 1 Type not found in cache: MyApp.Model.LocationModel. 
...\MyApp\MyApp.WindowsPhone\Views\LocationPage.xaml 10 5 MyApp.WindowsPhone 

在我的XAML中的错误在哪里,我定义我的DataContext行发生:

<Page 
.... 
DataContext="{Binding Source={StaticResource Locator}, Path=LocationViewModel}"> 

我LocationViewModel类的定义如下:

public class LocationViewModel : ViewModelBase 
{ 
    private RelayCommand _saveCommand; 
    private RelayCommand _cancelCommand; 

    #region Properties 

    public int Id 
    { 
     get 
     { 
      return this.Location.Id; 
     } 
    } 

    public string Title 
    { 
     get 
     { 
      return this.Location.Title; 
     } 
    } 

    public string Description 
    { 
     get 
     { 
      return this.Location.Description; 
     } 
    } 

    public string CreatedDateFormatted 
    { 
     get 
     { 
      return this.Location.CreatedDate.ToString("d"); 
     } 
    } 

    public string LastUpdatedDateFormatted 
    { 
     get 
     { 
      return Location.LastUpdatedDate.ToString("d"); 
     } 
    } 

    public string ImagePath 
    { 
     get 
     { 
      return this.Location.ImagePath; 
     } 
    } 

    public LocationModel Location 
    { 
     get; 
     private set; 
    } 

    #endregion 

    #region Constructors 

    public LocationViewModel(LocationModel model) 
    { 
     this.Location = model; 
     this.Location.PropertyChanged += (s, e) => 
      { 
       if (e.PropertyName == LocationModel.DescriptionPropertyName) 
       { 
        RaisePropertyChanged(() => Description); 
       } 
       if (e.PropertyName == LocationModel.TitlePropertyName) 
       { 
        RaisePropertyChanged(() => Title); 
       } 
       if (e.PropertyName == LocationModel.ImagePathPropertyName) 
       { 
        RaisePropertyChanged(() => ImagePath); 
       } 
       if (e.PropertyName == LocationModel.CreatedDateStringPropertyName) 
       { 
        RaisePropertyChanged(() => CreatedDateFormatted); 
       } 
       if (e.PropertyName == LocationModel.LastUpdatedDateStringPropertyName) 
       { 
        RaisePropertyChanged(() => LastUpdatedDateFormatted); 
       } 
      }; 
    } 

    #endregion 

    public RelayCommand SaveCommand 
    { 
     get 
     { 
      return this._saveCommand ?? (this._saveCommand = new RelayCommand(ExecuteSaveCommand)); 
     } 
    } 

    public RelayCommand CancelCommand 
    { 
     get 
     { 
      return this._cancelCommand ?? (this._cancelCommand = new RelayCommand(ExecuteCancelCommand)); 
     } 
    } 


    private void ExecuteSaveCommand() 
    { 

    } 

    private void ExecuteCancelCommand() 
    { 

    } 
} 

和我的财产我LocationViewModel在我的ViewModelLocator类中定义如下:

public LocationViewModel LocationViewModel 
    { 
     get 
     { 
      return ServiceLocator.Current.GetInstance<LocationViewModel>(); 
     } 
    } 

,并登记在ViewModelLocator的构造函数:

SimpleIoc.Default.Register<LocationViewModel>(); 

,当该代码被调用,它正确注册我LocationViewModel。

当点击我的“添加”按钮时,它将导航到将LocationViewModel设置为DataContext的页面,并在运行时发生错误。

我从LocationsViewModel(不LocationViewModel)调用一个调用了导航的代码是:

private void ExecuteAddCommand() 
    { 
     _navigationService.Navigate(typeof(LocationPage)); 
    } 

在调试上面,它会创建LocationPage,然后调用从ViewModelLocator的LocationViewModel,这是当同样的错误,在运行时间,但发生即

return ServiceLocator.Current.GetInstance<LocationViewModel>(); 

当我移动鼠标的,它显示以下内容:

Message: "Type not found in cache: MyApp.Model.LocationModel." 
InnerException: at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService 
(Type serviceType, String key) at 
GalaSoft.MvvmLight.Ioc.SimpleIoc.GetInstance[TService]() 
at Inventory.ViewModel.ViewModelLocator.get_LocationViewModel() 

其实,我刚刚意识到错误的产生要早得多,但不会引发错误。它实际上是在ViewModelLocator的构造函数中注册LocationViewModel时生成的:

SimpleIoc.Default.Register<LocationViewModel>(); 

任何想法?

谢谢。

回答

0

LocationViewModel构造函数依赖于LocationModelSimpleIoc容器无法创建视图模型实例,因为构造函数需要无法直接传递的LocationModel对象。您可能可以使用MVVMLight MessengerLocationModel对象与LocationViewModel构造函数分离。

public LocationViewModel() 
{ 
    MessengerInstance.Register<LocationModel>(this,m=>{model=m; 
      //PropertyChanged code 
    }); 
} 

LocationsViewModel,送你想通过只发送它在LocationViewModel构造函数使用LocationModel对象。

public void ExecuteAddCommand() 
{ 
    MessengerInstance.Send<LocationModel>(LocationModelObj); 
    _navigationService.navigate(tyepof(LocationPage)); 
} 

对于这个虽然取得成功,你需要注册LocationViewModel注册从LocationsViewModel发送对象之前接收LocationModel对象。因此,您需要使用SimpleIocRegister方法的重载立即创建视图模型。

SimpleIoc.Default.Register<LocationViewModel>(true); 
+0

我还没有机会尝试一下你的建议,只是还没有,因为它让我的思想,我结束了尝试别的东西根据你所提到的这也起作用。我不确定这是否是正确的解决方案,因此我将花更多时间调查这两种解决方案,并在完成后相应更新。 – Thierry

0

基于什么@Shridhar说The SimpleIoc container couldn't create the view model instance as the constructor requires a LocationModel object which you can't pass directly,我想我会尝试加入一个参数的构造函数,但我得到了另一个错误即

无法注册:在LocationViewModel发现多个构造函数,但标有 无PreferredConstructor。

所以我标志着我与PreferredConstructor这样参数的构造函数:

[PreferredConstructor] 
    public LocationViewModel() 
    { 

    } 

这个整理我的问题,而是提到@Shridar,我不知道这是否是正确的解决方案,因此我会花更多的时间调查,看看这是否按预期工作,没有任何副作用。

我会尽快更新。

0

我在尝试使用MVVMLight DialogService时也遇到过类似的错误;该解决方案是,以确保它是注册于ViewModelLocator

public ViewModelLocator() 
{ 
    SimpleIoc.Default.Register<IDialogService, DialogService>(); 
}