C# 在视图模型Windows phone 8中获取导航数据

C# 在视图模型Windows phone 8中获取导航数据,c#,windows-phone-8,mvvm,mvvm-light,C#,Windows Phone 8,Mvvm,Mvvm Light,我在项目中使用Mvvmlight INavigationService,从视图模型导航到视图,并将对象作为参数传递,如何在视图模型或代码中获取 我的视图模型定位器 private static INavigationService CreateNavigationService() { var navigationService = new NavigationService(); navigationService.Configure("topupd

我在项目中使用Mvvmlight INavigationService,从视图模型导航到视图,并将对象作为参数传递,如何在视图模型或代码中获取

我的视图模型定位器

private static INavigationService CreateNavigationService()
    {
        var navigationService = new NavigationService();

        navigationService.Configure("topupdetails", new System.Uri("/Views/TopUpDetails.xaml", System.UriKind.Relative));

        return navigationService;
    }
执行查看模型命令

private void OnTapCommand(MyModel tappedItem)
    {
        if (tappedItem._verified)
        {
        SimpleIoc.Default.GetInstance<INavigationService>().NavigateTo("topupdetails",tappedItem);

        }
在查询字符串中,我只是像“e-fghfdsfsd”这样的值,但不能强制转换到我的类对象,我可以在视图模型或代码隐藏中获取值吗?这是最好的方法,代码隐藏中的代码最少

Mvvmlight版本:Mvvmlight 5


您可以使用NavigationService评估NavigationContext:

GalaSoft.MvvmLight.Views.NavigationService _navigationService = [..] // Get your NS instance here or create a new one.
var m = _navigationService.GetAndRemoveParameter(NavigationContext);
// Try to cast:
MyModel model = m as MyModel;
// Deny if it's not a valid object
if (model == null)
    return;

e、 参数对我不可用,例如。内容可用-它仅包含导航页面中的网格、堆栈面板。我正在使用mvvmlight Inavigation从视图模型传递对象谢谢,这在viewmodel中的任何位置都可以工作?目前我正在使用messenger服务传递对象。谢谢,如果我必须获得导航上下文,我必须在页面代码中隐藏,那么我应该获取视图模型实例并将它们传递给所选项目,那么我有没有更简单的方法来实现这一点,我的问题是两个页面的视图模型是相同的。现在我认为messenger是最好的方法,请让我知道您的想法,使用messenger应该适合这种情况。我不知道你的应用程序的体系结构,但你不能总是每页都有一个VM。我有一个列表视图页面,并将项添加到列表视图页面,两者都插入到单个VM,目前我以上述方式实现,就像你的方式,我以视图模型的形式获取数据上下文,并从代码后面传递对象。此dataContext作为MyViewModel,并将对象从代码隐藏传递给viewmodel。我不知道它有任何已知的问题
GalaSoft.MvvmLight.Views.NavigationService _navigationService = [..] // Get your NS instance here or create a new one.
var m = _navigationService.GetAndRemoveParameter(NavigationContext);
// Try to cast:
MyModel model = m as MyModel;
// Deny if it's not a valid object
if (model == null)
    return;