Mvvm light - 无法配置导航

时间:2014-12-27 13:14:47

标签: c# xaml windows-phone-8 mvvm mvvm-light

我对navigationServicenavigationService.Configure

有疑问

在我的ViewModelLocator构造函数中,我有:

var navigationService = new NavigationService();
navigationService.Configure("MvvmView",new Uri("/MvvmView1.xaml"));
navigationService.Configure("Main",new Uri("/MainPage.xaml"));

SimpleIoc.Default.Register<INavigationService>(() => navigationService);

MvvmView1.xaml位于我的主文件夹中,与MainPage.xaml相同。

奇怪的是 - 它给我一个错误

  

{System.TypeInitializationException:类型初始值设定项   'SpaceQuiz.ViewModel.ViewModelLocator'引发了一个异常。 ---&GT;   System.UriFormatException:无效的URI:URI的格式可以   没有确定。

     

at System.Uri.CreateThis(String uri,Boolean   dontEscape,UriKind uriKind)在System.Uri..ctor(String uriString)   在SpaceQuiz.ViewModel.ViewModelLocator..cctor()---内部结束   异常堆栈跟踪--- at   System.RuntimeMethodHandle.InvokeMethod(Object target,Object []   参数,签名sig,布尔构造函数)at   System.Reflection.RuntimeConstructorInfo.Invoke(的BindingFlags   invokeAttr,Binder binder,Object []参数,CultureInfo文化)
  在   MS.Internal.TypeProxy&LT;&GT; c__DisplayClass32.b__2c()   在MS.Internal.TypeProxy.CreateInstance(UInt32 customTypeId)at   MS.Internal.XamlManagedRuntimeRPInvokes.CreateInstance(XamlTypeToken   inXamlType,XamlQualifiedObject&amp; NEWOBJECT)}

我尝试了很多组合,例如:

navigationService.Configure("MvvmView",new Uri("MvvmView1.xaml"));
navigationService.Configure("MvvmView",new Uri("/MvvmView1"));
navigationService.Configure("MvvmView",new Uri("MvvmView1"));
navigationService.Configure("MvvmView",new Uri("./MvvmView1.xaml"));
navigationService.Configure("MvvmView",new Uri("/MvvmView"));

等 - 没有任何成功..

如何在mvvm灯中注册导航?

任何帮助都是有价值的。

此致

1 个答案:

答案 0 :(得分:1)

如果你正在使用Mvvm Light v 5.0.2我相信你不应该使用URI作为第二个参数,它应该是Typeof(view)。首先,为每个视图类定义一个字符串常量,然后像这样配置 navigationservice

//This is in the ViewModelLocator.cs

// Define one key for each view/page. 
// You can call them anything but I use my view/class name followed by "Key"

public const string MvvmView1Key  = "MvvmView1"; 

var nav = new NavigationService();

//Updated to reflect SilverLight instead of Store App.
nav.Configure("MvvmView1", new Uri("/MvvmView1.xaml", UriKind.RelativeOrAbsolute));

// Assuming that your view class is called MvvmView1.