繁体   English   中英

如何在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 {MyProjectName}.ViewModels.{MyPageViewModel}

要在ViewModel中使用INavigationService ,只需将其添加到Constructor中即可。 请记住,它是一个命名服务,因此它必须命名为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

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

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

Container.RegisterType<ISomeService, SomeServiceImplementation>();

Unity容器将自动注入构造函数

在目标类中定义一个构造函数,该构造函数将依赖类的具体类型作为参数。 Unity容器将实例化并注入实例。

PrismApplication在容器中注册INavigationService ,因此,据我所知,您不需要注册自己的实例,您可以在视图模型的构造函数中添加INavigationService类型的参数,并且unity容器将注入已注册类型的实例。 重要的是,您应该为参数指定一个确切的navigationService名称,因为Prism “期望”导航服务的此类参数名称。

暂无
暂无

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

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