0
通常我會使用一個通用的導航:導航到現有的視圖模型
navigationService
.UriFor<PivotPageViewModel>()
.Navigate();
但我怎麼做,如果我有一個現有的視圖模型實例?
通常我會使用一個通用的導航:導航到現有的視圖模型
navigationService
.UriFor<PivotPageViewModel>()
.Navigate();
但我怎麼做,如果我有一個現有的視圖模型實例?
檢查源透露這UriBuilder
public Uri BuildUri()
{
var viewType = ViewLocator.LocateTypeForModelType(typeof(TViewModel), null, null);
if(viewType == null)
{
throw new InvalidOperationException(string.Format("No view was found for {0}. See the log for searched views.", typeof(TViewModel).FullName));
}
var packUri = ViewLocator.DeterminePackUriFromType(typeof(TViewModel), viewType);
var qs = BuildQueryString();
return new Uri(packUri + qs, UriKind.Relative);
}
讓你無論是在navigationService
用gettype,並使用虛擬機的類型使用反射MakeGenericMethod
:
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod.aspx
或者你可以使用上面的代碼,用viewModel.GetType()
代替typeof
調用(但是你將不得不在UriBuilder上重寫BuildQueryString,因爲它是私有的 - 與選項1!)
選擇是你的!