我在项目中使用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);
}
Page I导航到
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.Any())
{
var item = NavigationContext.QueryString.Values.First();
}
base.OnNavigatedTo(e);
}
在查询字符串中我只是像这样的“e-fghfdsfsd”,但是不能转换为我的类对象,我可以在视图模型中或在代码隐藏中获取值,这是最好的方法,使用最少的代码代码隐藏?
Mvvmlight版本:Mvvmlight 5 Laurent Mvvmlight 5 Announcement
答案 0 :(得分:2)
您可以使用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;