2

当我的 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();
        }
    }
4

2 回答 2

2

我知道这早就应该了,但这是我的 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();
        }
    }
}

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

于 2013-10-04T11:54:10.800 回答
0

我今天在我的Xamarin项目中遇到了同样的错误。给出的实际错误是“ System.Reflection.TargetInvocationException:'调用目标已引发异常。' “然后当我查找时,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. 希望能帮助到你。

于 2019-08-07T14:18:14.387 回答