问题
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.
I have implemented based on this
My View model locator
private static INavigationService CreateNavigationService()
{
var navigationService = new NavigationService();
navigationService.Configure("topupdetails", new System.Uri("/Views/TopUpDetails.xaml", System.UriKind.Relative));
return navigationService;
}
View Model command executes
private void OnTapCommand(MyModel tappedItem)
{
if (tappedItem._verified)
{
SimpleIoc.Default.GetInstance<INavigationService>().NavigateTo("topupdetails",tappedItem);
}
Page I navigated to
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.Any())
{
var item = NavigationContext.QueryString.Values.First();
}
base.OnNavigatedTo(e);
}
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
I have also referred this
回答1:
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;
来源:https://stackoverflow.com/questions/28615841/get-navigation-data-in-view-model-windows-phone-8