【问题标题】:MVVM Light "Type Not Found in cache"MVVM 灯“在缓存中找不到类型”
【发布时间】:2014-05-11 10:15:57
【问题描述】:

我正在尝试将我的 Windows Phone 8 Silverlight 应用程序转换为 8.1 Phone 应用程序,作为通用应用程序的一部分。我不知道这是否相关,因为这是我第一次尝试正确实现视图模型。我想在 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
    }
}

我认为 XAML 有这个

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

我的应用有这个

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

任何想法我做错了什么?

【问题讨论】:

    标签: c# mvvm-light win-universal-app


    【解决方案1】:

    答案是一个相当简单的错误。该位未在设计模式下执行

    SimpleIoc.Default.Register<HomePageViewModel>();
    

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

    【讨论】:

    • 这里相同,但我还有另外几十个其他 ViewModel 以相同的方式注册。不明白为什么这个会抛出这个错误。奇怪的是它是一个空的,并且继承自与其他抽象类相同的抽象类。使困惑。还有其他建议吗?
    • 当你说一些代码没有在设计模式下执行时,你不是说你是如何修复它的?你能解释一下吗?
    • 我的 SimpleIoc.Default.Register&lt;HomePageViewModel&gt;(); 代码在一个从未在设计模式下执行的 if 语句中。查看 OP 中的代码块
    【解决方案2】:

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

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

    我的注册行是这样的:

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

    我向MyConcreteClass 添加了一个无参数构造函数,然后对其应用了[PreferredConstructor] 属性(该属性在GalaSoft.MvvmLight.Ioc 命名空间中可用)并解决了这个问题。

    希望这对未来的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 2016-06-23
      • 2013-06-29
      • 1970-01-01
      • 2019-05-13
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多