SimpleIoC - Type not found in cache: Windows.UI.Xaml.Controls.Frame

扶醉桌前 提交于 2019-12-06 09:39:20

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

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

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.

I was getting the same error today in my Xamarin project. The actual error given was "System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'" and then when I look up the InnerException I could see the actual error, which is Type not found in cache.

It was a silly mistake that I was using DataService instead of IDataService for the Constructor Dependency Injection.

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

And just for your information, this is how I registered my service.

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

So the issue was fixed after changing to IDataService. Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!