【问题标题】:SimpleIoC - Type not found in cache: Windows.UI.Xaml.Controls.FrameSimpleIoC - 在缓存中找不到类型:Windows.UI.Xaml.Controls.Frame
【发布时间】:2013-06-18 18:09:12
【问题描述】:

当我的 ViewModel 第一次被 SimpleIoC 实例化时,我遇到了以下错误。我相信我已经按应有的方式设置了容器,但由于某种原因,我仍然收到以下错误。任何想法或帮助将不胜感激。

    Microsoft.Practices.ServiceLocation.ActivationException was unhandled by user code
  HResult=-2146233088
  Message=Type not found in cache: Windows.UI.Xaml.Controls.Frame.
  Source=GalaSoft.MvvmLight.Extras
  StackTrace:
       at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type serviceType, String key) in c:\Users\Public\Downloads\CodePlex\MVVMLight\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (NET35)\Ioc\SimpleIoc.cs:line 532
       at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetService(Type serviceType) in c:\Users\Public\Downloads\CodePlex\MVVMLight\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (NET35)\Ioc\SimpleIoc.cs:line 768
       at GalaSoft.MvvmLight.Ioc.SimpleIoc.MakeInstance[TClass]() in c:\Users\Public\Downloads\CodePlex\MVVMLight\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (NET35)\Ioc\SimpleIoc.cs:line 708
  InnerException:

以下是我与此相关的代码片段:

ViewModelLocator.cs(位于我的 Win8 项目中)

public class ViewModelLocator
{
    /// <summary>
    /// Initializes a new instance of the ViewModelLocator class.
    /// </summary>
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

        if (ViewModelBase.IsInDesignModeStatic)
        {
            // Create design time view services and models
            //SimpleIoc.Default.Register<IDataService, DesignDataService>();
        }
        else
        {
            // Create run time view services and models
            //SimpleIoc.Default.Register<IDataService, DataService>();
            SimpleIoc.Default.Register<INavigationService, NavigationService>();
            SimpleIoc.Default.Register<IParseService, ParseService>();
            SimpleIoc.Default.Register<IServiceHandler, ServiceHandler>();
        }

        SimpleIoc.Default.Register<MainViewModel>();
        SimpleIoc.Default.Register<ActionViewModel>();
    }

    public MainViewModel MainVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public ActionViewModel ActionVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<ActionViewModel>();
        }
    }

    public static void Cleanup()
    {
        // TODO Clear the ViewModels
    }
}

MainViewModel.cs 构造函数

public class MainViewModel : ViewModelBase
{
    #region Variables

    private readonly INavigationService _navigationService;
    private readonly IParseService _parseService;

    #endregion

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(INavigationService navigationService, IParseService parseService)
    {
        if (IsInDesignMode)
        {
            // Code runs in Blend --> create design time data.
        }
        else
        {
            _navigationService = navigationService;
            _parseService = parseService;
            BuildCommonData();
        }
    }

【问题讨论】:

  • 我能够弄清楚我的问题是什么!该死的复制/粘贴。我的 NavigationService 实现有一个在 Frame 中传递的构造函数。这显然在容器注册中没有考虑到。我只是删除了不需要的结构,瞧,问题解决了。如果需要,我可以分享该代码。
  • 您应该添加解决方案以及违规代码作为答案,然后将其设置为接受的答案。
  • 添加解决方案作为答案!

标签: windows-8 windows-store-apps winrt-xaml mvvm-light ioc-container


【解决方案1】:

我知道这早就应该了,但这是我的 NavigationService 类实现中的违规代码。

NavigationService 类(之前)

public class NavigationService : INavigationService
{
    /// <summary>
    /// Gets the root frame.
    /// </summary>
    private Frame RootFrame;

    public NavigationService(Frame rootFrame)
    {
        RootFrame = rootFrame;
    }

    public event NavigatingCancelEventHandler Navigating;

    public void Navigate<T>(object parameter = null)
    {
        var type = typeof(T);
        RootFrame.Navigate(type, parameter);
    }

    public void Navigate(string type, object parameter = null)
    {
        RootFrame.Navigate(Type.GetType(type), parameter);
    }

    public void GoBack()
    {
        if (RootFrame.CanGoBack)
        {
            RootFrame.GoBack();
        }
    }

    public void GoForward()
    {
        if (RootFrame.CanGoForward)
        {
            RootFrame.GoForward();
        }
    }
}

我只是取出构造函数,并将 RootFrame 私有变量设为属性。像这样:

public class NavigationService : INavigationService
{
    /// <summary>
    /// Gets the root frame.
    /// </summary>
    private static Frame RootFrame
    {
        get { return Window.Current.Content as Frame; }
    }

    public event NavigatingCancelEventHandler Navigating;

    public void Navigate<T>(object parameter = null)
    {
        var type = typeof(T);
        RootFrame.Navigate(type, parameter);
    }

    public void Navigate(string type, object parameter = null)
    {
        RootFrame.Navigate(Type.GetType(type), parameter);
    }

    public void GoBack()
    {
        if (RootFrame.CanGoBack)
        {
            RootFrame.GoBack();
        }
    }

    public void GoForward()
    {
        if (RootFrame.CanGoForward)
        {
            RootFrame.GoForward();
        }
    }
}

简单,我知道,但希望它有一些用处。

【讨论】:

    【解决方案2】:

    我今天在我的 Xamarin 项目中遇到了同样的错误。给出的实际错误是“System.Reflection.TargetInvocationException: 'Exception has been throwed by the target of an invocation.'” 然后当我查找InnerException 时,我可以看到实际错误,这是Type not found in cache

    我使用DataService 而不是IDataService 进行构造函数依赖注入是一个愚蠢的错误。

    public SearchViewModel(DataService dataService, IErrorLoggingService errorLoggingService, IDialogService dialogService, IResourceService resourceService, INavigationService navigationService) {
        SearchCommand = new AsyncRelayCommand <SearchFilter>(SearchAsync);
        DataService = dataService;
        ErrorLoggingService = errorLoggingService;
        DialogService = dialogService;
        ResourceService = resourceService;
        NavigationService = navigationService;
        CancelCommand = new RelayCommand(Cancel);
    }
    

    仅供参考,这就是我注册服务的方式。

    SimpleIoc.Default.Register<IDataService, DataService>();
    

    所以改成IDataService后问题就解决了。希望对您有所帮助。

    【讨论】:

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