Eldho
Eldho

Reputation: 8273

Get Navigation data in View Model Windows phone 8

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

Upvotes: 0

Views: 476

Answers (1)

Fred
Fred

Reputation: 3362

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;

Upvotes: 2

Related Questions