Simple Injector和MVVM Null对象引用

时间:2017-10-10 18:09:19

标签: c# wpf xaml mvvm simple-injector

首先,新的MVVM和WPF,构建我的第一个应用程序跟随MVVM。我试图在互联网上找到答案,但没有任何结果,所以决定在这里问。

我在MainWindow中有以下构造函数:

container = new SimpleInjector.Container();

container.Register<IContext, ApplicationContext>(Lifestyle.Singleton);
            container.Register<SearchOrderViewModel>();

_searchOrderViewModel = container.GetInstance<SearchOrderViewModel>();

NavigationCommand = new RelayCommand<string>(OnNavigation);

然后我使用带有Command的按钮,用XML将Content设置为ContentControl:

private void OnNavigation(string destination)
        {
            switch (destination)
            {
                case "orderSearch":
                    CurrentViewModel = _searchOrderViewModel;
                    break;
                default:
                    break;
            }
        }

最后是我的SearchOrderViewModel构造函数

private IContext InstanceContext;


public SearchOrderViewModel(IContext applicationContext)
{
       InstanceContext = applicationContext;

}

我的问题是,当CurrentView更改为SearchOrderViewModel时,它返回的System.NullReferenceException:&#39; Object引用未设置为对象的实例。&#39;来自SearchOrderView.xaml中的InitializeComponent(),基本上是:

public partial class SearchOrderView : UserControl
    {
        public SearchOrderView()
        {
            InitializeComponent(); <----- Exception happens here
        }
    }

Stack Tracking如果有帮助:

 at System.DefaultBinder.BindToMethod(BindingFlags bindingAttr, MethodBase[] match, Object[]& args, ParameterModifier[] modifiers, CultureInfo cultureInfo, String[] names, Object& state)
   at MS.Internal.Xaml.Runtime.DynamicMethodRuntime.CreateInstanceWithCtor(Type type, Object[] args)
   at MS.Internal.Xaml.Runtime.DynamicMethodRuntime.CreateInstanceWithCtor(XamlType xamlType, Object[] args)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
   at MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.CreateInstance(XamlType xamlType, Object[] args)
   at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
   at System.Xaml.XamlObjectWriter.WriteEndObject()
   at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
   at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)

我无法弄清楚我自己的错误并花了几个小时,但我没有想法。我希望有人可以提供一些出错的信息吗?

编辑::如果我从OrderViewModel(IContext applicationContext)中删除公共IContext applicationContextSearch正在运行,但我无法传递我的应用程序上下文...

2 个答案:

答案 0 :(得分:0)

I think that the issue sounds like this it is in ApplicationContext, have you checked the constructor and seen if you are hitting it? It seams that maybe it has some decencies that are not registered.

答案 1 :(得分:0)

这是XAML,我从头开始重新连线并开始工作,由于某种原因无法找到我的ViewModel。

发现通过修改我的异常设置然后弹出了正确的异常。