Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVVM灯“;“在缓存中找不到类型”;_C#_Mvvm Light_Win Universal App - Fatal编程技术网

C# MVVM灯“;“在缓存中找不到类型”;

C# MVVM灯“;“在缓存中找不到类型”;,c#,mvvm-light,win-universal-app,C#,Mvvm Light,Win Universal App,我正在尝试将我的Windows Phone 8 Silverlight应用程序转换为8.1手机应用程序,作为通用应用程序的一部分。我不知道这是否相关,因为这是我第一次尝试正确实现视图模型。我想在Windows和Windows Phone中的视图之间共享数据。 不管怎样,这就是我的错误 Error 3 Type not found in cache: ScoreAlerts.ViewModel.FixturesViewModel. C:\Users\Dave\Documents\Vis

我正在尝试将我的Windows Phone 8 Silverlight应用程序转换为8.1手机应用程序,作为通用应用程序的一部分。我不知道这是否相关,因为这是我第一次尝试正确实现视图模型。我想在Windows和Windows Phone中的视图之间共享数据。 不管怎样,这就是我的错误

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
这就是我的视图模型定位器的外观

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
    }
}
我的应用程序有这个

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


你知道我做错了什么吗?

答案很简单。此位未在设计模式下执行

SimpleIoc.Default.Register<HomePageViewModel>();
simpleoc.Default.Register();
SimpleIoc.Default.Register()的代码;位于从未在设计模式下执行的if语句中


在我的例子中,目标类没有实现无参数构造函数。该类包含的唯一构造函数接受字节类型参数,因此我得到:

在缓存中找不到类型:System.Byte

我的注册行是这样的:

SimpleIoc.Default.Register<IMyInterface, MyConcreteClass>();
simpleoc.Default.Register();
我在
MyConcreteClass
中添加了一个无参数构造函数,然后将
[PreferredConstructor]
属性应用到它(该属性在
GalaSoft.MvvmLight.Ioc
命名空间中提供)并解决了这个问题


希望这对以后的人有所帮助。

这里也一样,但我还有几十个其他ViewModels以同样的方式注册。我不明白为什么这个会抛出这个错误。奇怪的是,它是一个空类,继承自与其他抽象类相同的抽象类。困惑的还有其他建议吗?当你说有一点代码没有在设计模式下执行时,你不是在说你是如何修复它的?你能解释一下吗?
SimpleIoc.Default.Register()的代码位于从未在设计模式下执行的if语句中。请参见OP中的代码块
SimpleIoc.Default.Register<IMyInterface, MyConcreteClass>();