我试图把一个通用的应用程序一起,我使用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>();
任何想法?
谢谢。
我还没有机会尝试一下你的建议,只是还没有,因为它让我的思想,我结束了尝试别的东西根据你所提到的这也起作用。我不确定这是否是正确的解决方案,因此我将花更多时间调查这两种解决方案,并在完成后相应更新。 – Thierry