简体   繁体   English

MVVM Light:关于提取RelayCommand导航参数

[英]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.我是 MVVM 的新手,所以如果这个问题有一个简单的答案,我深表歉意,但我无法通过搜索谷歌来解决这个问题。

So, basically, I'm using MVVM Light to make a Windows 10 Universal app.所以,基本上,我使用 MVVM Light 来制作 Windows 10 通用应用程序。 Navigation using the method of ViewModelLocator and it works fine.使用 ViewModelLocator 方法导航,效果很好。 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?但是如何在目标页面上提取这个p呢? So far I've figured that到目前为止我已经想到了

  1. This parameter goes into the OnNavigatedTo method of the target page's View, which I can then pass to the ViewModel.此参数进入目标页面视图的OnNavigatedTo方法,然后我可以将其传递给 ViewModel。 But that seems kind of not in-line with what MVVM is about.但这似乎与 MVVM 的内容不符。 I don't even have an OnNavigatedTo in my second page and it loads just fine.我的第二页中甚至没有OnNavigatedTo并且它加载得很好。

  2. The other method is to use the Messenger method of MVVMLight to get it in the second page's VM, which seems cleaner.另一种方法是使用MVVMLight的Messenger方法在第二页的VM中获取,看起来更简洁。 But if this is the only right way to do, what's the point of passing a parameter in the RelayCommand at all?但如果这是唯一正确的方法,那么在 RelayCommand 中传递参数有什么意义呢?

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.您可以构建和使用自己的 NavigationService。 OR improve the current one:或改进当前的:

Create BindablePage, which inherit from Page.创建 BindablePage,它继承自 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:创建 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.在您的 Viewmodel 中实现 INavigable,您将在您的 viewmodel 中处理 OnNavigatedTo,它可以访问您发送的 p 参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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