MVVM指示灯和导航"对象引用未设置为对象的实例。"

时间:2015-09-21 23:40:52

标签: c# mvvm

我设置了ViewModelLocator,如此:

    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        SimpleIoc.Default.Register<INavigationService>(() => this.CreateNavigationService());
        SimpleIoc.Default.Register<IDialogService, DialogService>();
        SimpleIoc.Default.Register<IUnitOfWork, InfiniUnitOfWork>();
        SimpleIoc.Default.Register<MainViewModel>();
        SimpleIoc.Default.Register<TrendingGagsViewModel>();

    }

    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public TrendingGagsViewModel TrendingGags
    {
        get
        {
            return ServiceLocator.Current.GetInstance<TrendingGagsViewModel>();
        }
    }


    private INavigationService CreateNavigationService()
    {
        var navigationService = new NavigationService();
        navigationService.Configure("TrendingGag", typeof(TrendingGagsViewModel));
        return navigationService;
    }

我的MainPage和我的TrendingGagPage设置如下:

<Page
    x:Class="_Blah.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:_Blah"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
    Name="Page"
    Background="Black">
...

<Page x:Class="_Blah.TrendingGagsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:_Blah"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    DataContext="{Binding Source={StaticResource Locator}, Path=TrendingGags}"
    Background="Black">
...

我的MainViewModel像这样注入INavigationService

    IUnitOfWork unitOfWork;
    INavigationService navigationService;

    public MainViewModel(IUnitOfWork uow, INavigationService navigationService)
    {
        this.unitOfWork = uow;
        this.navigationService = navigationService;
    }

并且有这样的属性:

    public RelayCommand<Page> ButtonCommand_LoadGagPage
    {
        get
        {
            return new RelayCommand<Page>(page => navigationService.NavigateTo("TrendingGag", page));
        }
    }

当我点击MainNavigation页面上的按钮时......它会转到RelayCommand ...然后抛出以下错误:

  

类型&#39; System.NullReferenceException&#39;的例外情况发生在   GalaSoft.MvvmLight.Platform.dll但未在用户代码中处理

     

附加信息:对象引用未设置为的实例   对象

this.navigationService显示对象存在?

可能出现什么问题?

我的堆栈跟踪如下所示:

   at _Blah._Gag_XamlTypeInfo.XamlUserType.ActivateInstance()
   at Windows.UI.Xaml.Controls.Frame.Navigate(Type sourcePageType, Object parameter)
   at GalaSoft.MvvmLight.Views.NavigationService.NavigateTo(String pageKey, Object parameter)
   at _Blah.ViewModels.MainViewModel.<get_ButtonCommand_LoadGagPage>b__6_0(Page page)

0 个答案:

没有答案