2014-12-27 67 views
0

我有navigationServicenavigationService.ConfigureMVVM光 - 無法配置導航

一個問題在我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' 的類型初始值引發異常。 ---> System.UriFormatException:無效的URI:URI的格式可能不是 。的內

在System.Uri.CreateThis(字符串URI,布爾 dontEscape,UriKind uriKind)在System.Uri..ctor(字符串uriString中) 在SpaceQuiz.ViewModel.ViewModelLocator..cctor()---完 異常堆棧跟蹤---在 System.RuntimeMethodHandle.InvokeMethod在 System.Reflection.RuntimeConstructorInfo.Invoke(的BindingFlags invokeAttr,捆紮機粘合劑,對象(對象目標,對象[] 參數,簽名Sig,布爾構造)[]參數,CultureInfo culture)
at MS.Internal.TypeProxy。 <> c__DisplayClass32.b__2c() 在MS.Internal.TypeProxy.CreateInstance(UInt32的customTypeId)在 MS.Internal.XamlManagedRuntimeRPInvokes.CreateInstance(XamlTypeToken inXamlType,XamlQualifiedObject & 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

如果你使用MVVM光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. 
+0

我使用的版本'大會GalaSoft.MvvmLight.Platform.dll,v5.0.2.32241',但我不能使用Typeof(View)' - 只有'public void Configure(string key,Uri targetUri);'' – whoah

+0

哦 - 好的。我的錯誤對不起(我正在考慮WP81商店應用程序,而不是Silverlight)。在這種情況下,試試這個:nav.Configure(「MvvmView1」,new Uri(「/ MvvmView1.xaml」,UriKind.RelativeOrAbsolute));這對我來說可以。 – klasha