导航到现有视图模型

时间:2013-08-22 19:53:29

标签: c# windows-phone-8 caliburn.micro

通常我会使用通用导航:

navigationService
    .UriFor<PivotPageViewModel>()  
    .Navigate();  

但是,如果我有一个现有的视图模型实例,该怎么办?

1 个答案:

答案 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!)

选择权在你手中!