如何解决System.Reflection.TargetInvocationException'错误?

时间:2015-05-21 00:52:33

标签: c# viewmodel mvvm-light ioc-container win-universal-app

我在我的应用程序中使用MVVM Light库,它提供了一个名为ViewModelLocator的Ioc容器。但是当我在定位器中为新视图模型设置第二个属性时,我得到ViewSubjectGradeViewModel

我通过查看内部异常来调试它,并且似乎抛出了这个错误,因为我没有在我的定位器中指定将参数传递到另一个VM。

有没有人知道我如何在locator类中设置namespace LC_Points.ViewModel { /// <summary> /// This class contains static references to all the view models in the /// application and provides an entry point for the bindings. /// </summary> public class ViewModelLocator { public ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIoc.Default.Register<MainViewModel>(); SimpleIoc.Default.Register<ViewSubjectGradeViewModel>(); } public MainViewModel MainPage { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } } public ViewSubjectGradeViewModel ViewSubjectGradePage { get { return ServiceLocator.Current.GetInstance<ViewSubjectGradeViewModel>(); } } public static void Cleanup() { // TODO Clear the ViewModels } } } 属性来解释传递给该模型的ScoreModel参数?

这是ViewModelLocator供参考:

namespace LC_Points.ViewModel
{
    public class ViewSubjectGradeViewModel 
    {
        public ViewSubjectGradeViewModel(IEnumerable<ScoreModel> addedSubjectGradePairs)
        {
            this.AddedSubjectGradePairsCopy = addedSubjectGradePairs;           
        }

        //Property for collection passed from MainViewModel
        public IEnumerable<ScoreModel> AddedSubjectGradePairsCopy { get; set; }         
    }
}

这是我想在locator类中设置属性的新VM:

public virtual School School { get; set; }

1 个答案:

答案 0 :(得分:1)

您没有默认构造函数。

在C#中,无论何时指定带参数的构造函数,都会删除默认构造函数(空构造函数)。您可以指定默认构造函数(public ViewSubjectGradeViewModel(){})或告诉服务定位器如何实例化对象。