简体   繁体   中英

MVVM Light: About extracting the RelayCommand navigation parameter

I'm new to MVVM so I apologize if this is question has a simple answer, but I haven't been able to clear this up through searching Google.

So, basically, I'm using MVVM Light to make a Windows 10 Universal app. Navigation using the method of ViewModelLocator and it works fine. My question is about the accompanying parameter. I have the following method of navigating:

public RelayCommand<SelectionChangedEventArgs> SelectedItemCommand
    {
        get
        {
            return _selectedItemCommand
            ?? (_selectedItemCommand = new RelayCommand<SelectionChangedEventArgs>(
                 p => _navigationService.NavigateTo(ViewModelLocator.SecondPageKey, p)));

        }
    }

I go to the second page. But how do I extract this p on the target page? So far I've figured that

  1. This parameter goes into the method of the target page's View, which I can then pass to the ViewModel.方法,然后我可以将其传递给 ViewModel。 But that seems kind of not in-line with what MVVM is about. I don't even have an OnNavigatedTo in my second page and it loads just fine.

  2. The other method is to use the Messenger method of MVVMLight to get it in the second page's VM, which seems cleaner. But if this is the only right way to do, what's the point of passing a parameter in the RelayCommand at all?

Am I missing some kind of third technique? I'd love to learn something on this. Thanks in anticipation.

You can build and use your own NavigationService. OR improve the current one:

Create BindablePage, which inherit from Page. Inside that:

public class BindablePage : Page
{
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        var navigableViewModel = this.DataContext as INavigable;
        if (navigableViewModel != null)
            navigableViewModel.OnNavigatedTo(e);
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        var navigableViewModel = this.DataContext as INavigable;
        if (navigableViewModel != null)
            navigableViewModel.OnNavigatedFrom(e);
    }

    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        base.OnNavigatingFrom(e);

        var navigableViewModel = this.DataContext as INavigable;
        if (navigableViewModel != null)
            navigableViewModel.OnNavigatingFrom(e);
    }

}

Create INavigable:

public interface INavigable
{
    void OnNavigatedTo(NavigationEventArgs e);
    void OnNavigatedFrom(NavigationEventArgs e);
    void OnNavigatingFrom(NavigatingCancelEventArgs e);
    bool AllowGoBack();
}

Implement INavigable in your Viewmodel, and you will handle OnNavigatedTo inside your viewmodel, which have access to p Parameter you sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM