2014-12-30 74 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>(); 
}