1

I've just recently added a new View and ViewModel to my UWP project which uses MVVMLight and I've spent the last few hours trying to figure out between this and the new files but can't see anything different.

This might be a duplicate post MVVM Light “Type Not Found in cache", but I feel it was never really answered.

Anyway, I've got a dozen or more ViewModels declared in my ViewModelLocator and they all work as expected except for my new one which is defined exactly the same way as the others:

SimpleIoc.Default.Register<UnlockFeaturesPageViewModel>();

My ViewModel is declared as follows:

public class UnlockFeaturesPageViewModel : AbstractPageViewModel
{
    public UnlockFeaturesPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice, CurrentPageEnum currentPage) : 
           base(telemetryService, dataservice,
           CurrentPageEnum.UnlockFeatures)
}

As you can see it inherits from a AbstractPageViewModel and that abstract class is quite straight forward as well:

public abstract class AbstractPageViewModel : ViewModelBase, IPageViewModel
{
    public AbstractPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice, CurrentPageEnum currentPage) 
    {
        this._telemetryService = telemetryService;
        this._dataService = dataservice;
        this._currentPage = currentPage;
    }
}

And it contains other properties, methods that can be overwritten if needed. The ViewModelBase is the class from Galasoft.MVVMLight and IPageViewModel is a basic interface.

The error occurs when the InitialComponents(); in the CodeBehind is called and tried to initialized the DataContext which is defined as follows:

<Page x:Class="MyApp.Views.UnlockFeaturesPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:MyApp.Views"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      DataContext="{Binding Source={StaticResource Locator},
                    Path=UnlockFeaturesViewModel}" >

and the following code is then called from the ViewModelLocator and this is where the error occurs:

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

If I mouse my mouse over the , it displays the following error:

'UnlockFeaturesPageViewModel' threw an exception of type 
'System.Reflection.TargetInvocationException'

and when I execute the code, it throws the following error:

TargetInvocationException was unhandled by user code

An exception of type 'System.Reflection.TargetInvocationException' 
occurred in mscorlib.ni.dll but was not handled in user code.

and the InnerException contains the following details:

Message: Type not found in cache: MyApp.Constants.CurrentPageEnum.
Source: GalaSoft.MvvmLight.Extras
StackTrace: at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type
            serviceType, String key, Boolean cache) at
            GalaSoft.MvvmLight.Ioc.SimpleIoc.MakeInstance[TClass]()

So why am I getting this error regarding MyApp.Constants.CurrentPageEnum not being found in the cache. I've added a new value to my CurrentPageEnum to match the newly added page but according to the error this is cached somewhere and it is not updated. I could be totally wrong but I can't think of anything else as the code is identical to other ViewModels that work.

It's definitely related to my AbstractPageViewModel as if my UnlockFeatureViewModel inherits from ViewModelBase (from Galasoft.MVVMLight), it doesn't throw any errors.

Regarding as to why this could be a duplicate post is because in the other similar post, the developer mentioned that it simply did not execute the following line in debug mode but he doesn't mention how he fixed it.

SimpleIoc.Default.Register<UnlockFeaturesPageViewModel>();

My problem is that it appears to be executed in debug mode as I'm stepping through it and it goes to the next line and as mentioned the error only occurs when it is attempting to set the DataContext of my page to that specific view model.

Strangely no error occur in Release Mode!

Any ideas what could be causing this and how I can resolve this problem?

Thanks.

Community
  • 1
  • 1
Thierry
  • 6,142
  • 13
  • 66
  • 117

2 Answers2

2

Your UnlockFeaturesPageViewModel has an enum as dependency.

public class UnlockFeaturesPageViewModel : AbstractPageViewModel
{
    public UnlockFeaturesPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice, CurrentPageEnum currentPage) :  // This one, CurrentPageEnum do not belong here, it's enum and can't be resolved
           base(telemetryService, dataservice,
           CurrentPageEnum.UnlockFeatures)
}

Since the CurrentPageEnum is not a class that can be instantiated, the resolving fails.

You should remove the enum like this

public class UnlockFeaturesPageViewModel : AbstractPageViewModel
{
    public UnlockFeaturesPageViewModel(ITelemetryService telemetryService,
           IDataService dataservice) :
           base(telemetryService, dataservice,
           CurrentPageEnum.UnlockFeatures)
}
Tseng
  • 61,549
  • 15
  • 193
  • 205
  • Oh my.... I should have taken a break yesterday!! You were bang on! I can't believe I said all my other ViewModels were identical when they were not! They were defined as you have mentioned in your corrected answer! Many thanks. – Thierry Mar 09 '16 at 13:02
-1

I had this error before, this error is produced when the ServiceLocator tries to inject all of your services in the viewmodel. I'm not sure if it's an error of MVVM light or another thing. I solved this issue always using ServiceLocator in the constructor of my viewmodels for calling all my services without modify the constructor of all my viewmodels. Example:

Public MyViewModel()
{
  var myService= ServiceLocator.Current.GetInstance<IMyService>();

}
RicardoPons
  • 1,323
  • 9
  • 14
  • ServiceLocator shouldn't be used inside ViewModels as you create a dependency towards your IoC container. On top of that, your code becomes hard or impossible to unit test – Tseng Mar 09 '16 at 08:57