在View Model Windows Phone 8中获取导航数据 [英] Get Navigation data in View Model Windows phone 8
问题描述
我在项目中使用Mvvmlight INavigationService,从视图模型导航到视图,然后将对象作为参数传递,如何在视图模型或背后的代码中获取信息.
I'm using Mvvmlight INavigationService in the project, from view model I navigate to a view , and pass object as parameter ,how do I get either in the view model or in the code behind.
我的视图模型定位器
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);
}
我浏览到的页面
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.Any())
{
var item = NavigationContext.QueryString.Values.First();
}
base.OnNavigatedTo(e);
}
在查询字符串中,我只是像这样的"e-fghfdsfsd"值,但不能转换为我的类对象,我可以在视图模型中或在后面的代码中获取值,这是用最少的代码做到这一点的最佳方法在代码背后?
In query string I just value like this "e-fghfdsfsd" but cant cast to my class object , Can I get the value in view model or in the code-behind which is best way to do this , with a minimum code in code-behind ?
Mvvmlight Version: Mvvmlight 5 Laurent Mvvmlight 5 Announcement
推荐答案
您可以使用NavigationService评估NavigationContext:
You can use the NavigationService to evaluate the 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;
这篇关于在View Model Windows Phone 8中获取导航数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!