【问题标题】:Type not found in cache在缓存中找不到类型
【发布时间】: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


    【解决方案1】:

    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 以注册接收LocationModel 对象,然后再从LocationsViewModel 发送对象。因此,您需要使用 SimpleIocRegister 方法的重载立即创建您的视图模型。

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

    【讨论】:

    • 我还没有机会尝试你的建议,因为它让我思考,我最终根据你提到的内容尝试了其他东西,这也有效。我也不确定这是否是正确的解决方案,因此我将花更多时间研究这两种解决方案,并在完成后进行相应更新。
    【解决方案2】:

    根据@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 所提到的,我不确定这是否是正确的解决方案,所以我将花更多时间进行调查,看看它是否按预期工作并且没有任何副作用。

    我有东西会第一时间更新。

    【讨论】:

      【解决方案3】:

      我在尝试使用 MVVMLight DialogService 时也遇到了类似的错误;解决方案是确保它已在 ViewModelLocator 中注册

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-23
        • 1970-01-01
        • 2013-06-29
        • 1970-01-01
        • 2019-05-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多