Search code examples
c#windows-phone-8.1mvvm-lightnavigationservice

Navigation in WP 8.1 with MVVM Light


I can navigate to a DetailPage.xaml to show some more information, but I can't pass the object to the DetailVieModel and when I press back the App closes.

Here is my code:

In the ViewModelLocator

 public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  

        var navigationService = this.CreateNavigationService();
        SimpleIoc.Default.Register<INavigationService>(() => navigationService);

        SimpleIoc.Default.Register<MainViewModel>();
        SimpleIoc.Default.Register<DataService>();
        SimpleIoc.Default.Register<DetailViewModel>(true);
    }

The navigations, I have only one for the moment, this seems to work becouse I can navigate to DetailPage.xaml.

 private INavigationService CreateNavigationService()
    {
        var navigationService = new NavigationService();
        navigationService.Configure("DetailPage", typeof(DetailPage));

        return navigationService;
    }

This is what I do in the MainViewModel:

 public RelayCommand ShowDetailPage
    {
        get
        {
            return new RelayCommand(() =>
            {
                _INavigationService.NavigateTo("DetailPage");
            });
        }
    }

This is the object I want to pass to the DetailViewModel

 private Info _SelectedObject;
    public Info SelectedObject
    {
        get
        {
            return _SelectedObject;
        }
        set
        {
            _SelectedObject = value;
            if (value != null)
            {
                Messenger.Default.Send<Info>(value);
                _SelectedObject = null;
            }
            RaisePropertyChanged("SelectedObject");
        }
    }

This is what I do to trigger the command on MainPage.xaml

 <ListView Grid.Row="1"
                              ItemsSource="{Binding ObjectsList}">
                        <i:Interaction.Behaviors>
                            <core:EventTriggerBehavior EventName="Tapped">
                                <core:InvokeCommandAction Command="{Binding ShowDetailPage}" 
                                                          CommandParameter="{Binding SelectedObject}" />
                            </core:EventTriggerBehavior>
                        </i:Interaction.Behaviors>

And finaly on the DetailViewModel I ask the SelectedObject with Messenger.Register...

 Messenger.Default.Register<Info>(this, (selectedinfo) =>
        {
            _SelectedInfo = selectedinfo;
        });

Solution

  • To pass the object you could use:

    _navigationService.NavigateTo("DetailPage", _SelectedInfo);
    

    To handle back navigation, simply add the following in RootFrame_FirstNavigated event handler.

    #if WINDOWS_PHONE_APP
                Windows.Phone.UI.Input.HardwareButtons.BackPressed += (s, args) =>
                {
                    if (!rootFrame.CanGoBack)
                    {
                        return;
                    }
    
                    // Allow back navigation using Back button
                    args.Handled = true;
                    rootFrame.GoBack();
                };     
    #endif