通常我会使用通用导航:
navigationService
.UriFor<PivotPageViewModel>()
.Navigate();
但是,如果我有一个现有的视图模型实例,该怎么办?
答案 0 :(得分:1)
检查来源会在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,要么使用VM的类型使用反射MakeGenericMethod
:
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod.aspx
或者您可以使用上面的代码,将typeof
调用替换为viewModel.GetType()
(但您必须在UriBuilder上重写BuildQueryString,因为它是私有的 - 请使用选项1!)
选择权在你手中!