在视图模型中获取导航数据 Windows phone 8
Get Navigation data in View Model Windows phone 8
我在项目中使用 Mvvmlight INavigationService,我从视图模型导航到一个视图,并将对象作为参数传递,我如何在视图模型或后面的代码中获取。
I have implemented based on this
我的视图模型定位器
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" 但无法转换为我的 class 对象,我可以在视图模型或代码隐藏中获取值,这是最好的方法this ,代码隐藏中的代码最少 ?
Mvvmlight 版本:Mvvmlight 5 Laurent Mvvmlight 5 Announcement
您可以使用 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;
我在项目中使用 Mvvmlight INavigationService,我从视图模型导航到一个视图,并将对象作为参数传递,我如何在视图模型或后面的代码中获取。
I have implemented based on this
我的视图模型定位器
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" 但无法转换为我的 class 对象,我可以在视图模型或代码隐藏中获取值,这是最好的方法this ,代码隐藏中的代码最少 ?
Mvvmlight 版本:Mvvmlight 5 Laurent Mvvmlight 5 Announcement
您可以使用 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;