SimpleIoC-在缓存中找不到类型:Windows.UI.Xaml.Controls.Frame [英] SimpleIoC - Type not found in cache: Windows.UI.Xaml.Controls.Frame

查看:205
本文介绍了SimpleIoC-在缓存中找不到类型:Windows.UI.Xaml.Controls.Frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SimpleIoC第一次实例化ViewModel时,我遇到以下错误.我相信我已经按原样设置了容器,但是由于某些原因,我仍然遇到以下错误.任何想法或帮助将不胜感激.

I am running into the below error the first time my ViewModel is being instantiated by the SimpleIoC. I believe I have setup the container as it should be, but for some reason, I am still getting the below error. Any ideas or assistance would be very much appreciated.

    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:

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

Here are pieces of my code related to this:

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

ViewModelLocator.cs (Located in my Win8 project)

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构造函数

MainViewModel.cs Constructor

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类的实现中令人反感的代码.

I know this is long overdue, but here is the offending code in the implementation of my NavigationService class.

NavigationService类(之前)

NavigationService class (Before)

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私有变量设为一个属性.像这样:

I simply took out the constructor, and made the RootFrame private variable a property. Like so:

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();
        }
    }
}

简单,我知道,但是希望它能有所用.

Simple, I know, but hope it's of some use.

这篇关于SimpleIoC-在缓存中找不到类型:Windows.UI.Xaml.Controls.Frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆