【发布时间】:2015-02-26 07:30:21
【问题描述】:
我正在尝试将一个通用应用程序放在一起,并且我正在使用 mvvm light,但在编译我的应用程序时出现以下错误:
Error 1 Type not found in cache: MyApp.Model.LocationModel
...\MyApp.WindowsPhone\Views\LocationPage.xaml 10 5 MyApp.WindowsPhone
它确实编译成功,但我不知道是什么导致了问题。我发现了几篇关于 stackoverflow 的文章:
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>();
有什么想法吗?
谢谢。
【问题讨论】:
标签: c# mvvm mvvm-light win-universal-app