我試圖把一個通用的應用程序一起,我使用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