我在项目中使用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);
}
我导航到的页面
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.Any())
{
var item = NavigationContext.QueryString.Values.First();
}
base.OnNavigatedTo(e);
}
在查询字符串中,我的值只是这样的"e-fghfdsfsd",但不能强制转换为我的类对象。我可以在视图模型或代码背后获得值吗?这是最好的方法,代码背后的代码最少?
Mvvmlight版本:Mvvlight 5 Laurent Mvvlght 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;