MVVM Light "Type Not Found in cache"
Asked Answered
S

2

13

I'm trying to convert my Windows Phone 8 Silverlight application to an 8.1 Phone app as part of a universal app. I don't know if thats relevant because this is the first time I've tried to implement view models correctly. I'd like to share data between views in Windows and Windows Phone. Anyway, this is the error I'm getting.

Error   3   Type not found in cache: ScoreAlerts.ViewModel.FixturesViewModel.   C:\Users\Dave\Documents\Visual Studio 2012\Projects\Score Alerts\ScoreAlerts\ScoreAlerts.WindowsPhone\Pages\Fixtures.xaml   9   5   ScoreAlerts.WindowsPhone
Error   4   Type not found in cache: ScoreAlerts.ViewModel.HomePageViewModel.   C:\Users\Dave\Documents\Visual Studio 2012\Projects\Score Alerts\ScoreAlerts\ScoreAlerts.Shared\Pages\HomePage.xaml 34  9   ScoreAlerts.WindowsPhone

This is how my view model locator looks

public class ViewModelLocator
{
    /// <summary>
    /// Initializes a new instance of the ViewModelLocator class.
    /// </summary>
    public ViewModelLocator()
    {
        if (!ViewModelBase.IsInDesignModeStatic)
        {
            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<HomePageViewModel>();
            SimpleIoc.Default.Register<FixturesViewModel>();
        }
    }

    [SuppressMessage("Microsoft.Performance",
        "CA1822:MarkMembersAsStatic",
        Justification = "This non-static member is needed for data binding purposes.")]
    public HomePageViewModel Main
    {
        get
        {
            //return ServiceLocator.Current.GetInstance<HomePageViewModel>();
            return SimpleIoc.Default.GetInstance<HomePageViewModel>("default");
        }
    }

    [SuppressMessage("Microsoft.Performance",
        "CA1822:MarkMembersAsStatic",
        Justification = "This non-static member is needed for data binding purposes.")]
    public FixturesViewModel Fixtures
    {
        get
        {
            //return ServiceLocator.Current.GetInstance<FixturesViewModel>();
            return SimpleIoc.Default.GetInstance<FixturesViewModel>("default");
        }
    }

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

My view XAML has this

DataContext="{Binding Fixtures, Source={StaticResource Locator}}"

and my app has this

<viewModel:ViewModelLocator x:Key="Locator"
                         d:IsDataSource="True"/>

Any ideas what I'm doing wrong?

Snaggy answered 11/5, 2014 at 10:15 Comment(0)
S
18

The answer was a fairly simple mistake. This bit was not being executed in design mode

SimpleIoc.Default.Register<HomePageViewModel>();

My code for SimpleIoc.Default.Register(); was inside an if statement that was never executed in design mode.

Snaggy answered 19/5, 2014 at 20:38 Comment(3)
Same here and yet I've got another dozens of other ViewModels registered the same way. Don't understand why this one is throwing this error. Strange thing is that it's an empty one and inherits from the same abstract class as the others. Confused. Any other suggestions?Asphyxiate
When you say a bit of code was not being executed in design mode, you're not saying how you fixed it? Can you explain?Asphyxiate
My code for SimpleIoc.Default.Register<HomePageViewModel>(); was inside an if statement that was never executed in design mode. See the block of code in the OPSnaggy
K
3

In my case, the target class didn't implement a parameter-less constructor. The only constructor that the class contained accepted a byte type parameter, so I was getting:

Type not found in cache: System.Byte

My registration line was like this:

SimpleIoc.Default.Register<IMyInterface, MyConcreteClass>();

I added a parameter-less constructor to MyConcreteClass and then applied [PreferredConstructor] attribute to it (this attribute is available in GalaSoft.MvvmLight.Ioc namespace) and got rid of the issue.

Hope this helps someone down the road.

Kirkuk answered 14/3, 2018 at 18:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.