如何在 Xamarin.Forms 中使用 Prism 在页面之间导航?

How to navigate between pages using Prism in Xamarin.Forms?

我正在尝试将 INavigationService 注入我的 ViewModel,以便我可以在页面之间导航,但我不知道如何使用参数注册 ViewModel。这是我的应用程序:

<prism:PrismApplication xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Unity;assembly=Prism.Unity.Forms"
             x:Class="PrismDemo.App" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <prism:PrismApplication.Resources>
        <ResourceDictionary>

        </ResourceDictionary>
    </prism:PrismApplication.Resources>
</prism:PrismApplication>

和...

public partial class App
{
    INavigationService _navigationService;

    public App(IPlatformInitializer initializer = null) : base(initializer) { }

    protected override void OnInitialized()
    {
        InitializeComponent();

        _navigationService = NavigationService;
        NavigationService.NavigateAsync("MainNavigationPage/Start");
    }

    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<MainNavigationPage>();
        Container.RegisterTypeForNavigation<StartPage, StartPageViewModel>("Start");
        Container.RegisterTypeForNavigation<MiddlePage, MiddlePageViewModel>("Middle");
        Container.RegisterTypeForNavigation<LastPage, LastPageViewModel>("Last");
    }
}

如何将 _navigationService 注入 ViewModels?

感谢帮助

只要您遵循约定 {MyProjectName}.Views.{MyPage}{MyProjectName}.ViewModels.{MyPageViewModel}

,您实际上不必直接注册 ViewModel

要在 ViewModel 中使用 INavigationService,只需将其添加到构造函数中即可。请记住,它是一个命名服务,因此必须命名为 navigationService。您可以在 Samples Repo.

中查看几个示例
public class MyPageViewModel
{
    INavigationService _navigationService { get; }

    public MyPageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;

        NavigateCommand = new DelegateCommand<string>(OnNavigateCommandExecuted);
    }

    public DelegateCommand<string> NavigateCommand { get; }

    public async void OnNavigateCommandExecuted(string path) =>
        await _navigationService.NavigateAsync(path);
}

如果您正在使用 Prism,那么您应该从 PrismApplication:

继承您的 App

而不是 public partial class App 应该是 public partial class App : PrismApplication.

无需在 App class 中声明 INavigationService _navigationService;,因为它已在 PrismApplication 中声明为 NavigationService 属性。

Prism 使用 IUnityContainer 作为依赖注入框架,这对你来说意味着,你将在容器中注册:

Container.RegisterType<ISomeService, SomeServiceImplementation>();

Unity container will automatically inject in constructor:

Define a constructor in the target class that takes as a parameter the concrete type of the dependent class. The Unity container will instantiate and inject an instance.

PrismApplication 在容器中注册 INavigationService ,因此,据我了解,您不需要注册自己的实例,只需在其中添加 INavigationService 类型的参数即可视图模型和统一容器的构造函数将注入注册类型的实例。重要的是,你应该为你的参数提供一个准确的 navigationService 名称,因为 Prism "expects" 这样的导航服务参数名称。