MVVM Light抛出(你忘了调用> NavigationService.Configure?参数名:pageKey)异常

时间:2015-07-13 14:16:47

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

我正在使用mvvm light创建一个Universal winrt应用程序。在ViewModelLocator中,我在mvvm light的内置NavigationService中注册了我的视图

SimpleIoc.Default.Register<INavigationService>(() =>
{
    var navigationService = new NavigationService();
    navigationService.Configure("PreRegisterPage", typeof(PreRegisterPage));
    return navigationService;
});

但是当我尝试使用此代码导航到该页面时,

 _navigationService.NavigateTo("PreRegisterPage");

抛出此异常

  

没有这样的页面:PreRegisterPage。你忘记打电话了吗?   NavigationService.Configure?参数名称:pageKey

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

您可能忘记在INavigationService ctor中传递ViewModel个对象,此处ViewModel应如何显示:

  public class MainViewModel : ViewModelBase
{
    private INavigationService _navigationService;
    private RelayCommand _navigateCommand;
    public RelayCommand NavigateCommand
    {
        get
        {
            return _navigateCommand
                ?? (_navigateCommand = new RelayCommand(
                () =>
                {
                    _navigationService.NavigateTo("PreRegisterPage");
                }));
        }
    }
    public MainViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;           
    }
}