Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top