[英]How to navigate between pages using Prism in Xamarin.Forms?
I'm trying to inject INavigationService
into my ViewModel, so I can navigate between pages, but I don't know how to register ViewModel with parameter. 我正在尝试将
INavigationService
注入到ViewModel中,以便可以在页面之间导航,但是我不知道如何使用参数注册ViewModel。 Here's my App: 这是我的应用程序:
<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>
and... 和...
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");
}
}
How can I inject _navigationService
into ViewModels? 如何将
_navigationService
注入ViewModels?
Thanks for help 感谢帮助
You don't actually have to register the ViewModel directly as long as you have followed the convention {MyProjectName}.Views.{MyPage}
and {MyProjectName}.ViewModels.{MyPageViewModel}
只要遵循约定
{MyProjectName}.Views.{MyPage}
和{MyProjectName}.ViewModels.{MyPageViewModel}
,您实际上不必直接注册ViewModel {MyProjectName}.ViewModels.{MyPageViewModel}
To use INavigationService
in the ViewModel simply add it to the Constructor. 要在ViewModel中使用
INavigationService
,只需将其添加到Constructor中即可。 Remember it is a named service so it must be named navigationService
. 请记住,它是一个命名服务,因此它必须命名为
navigationService
。 You can check out several examples in the Samples Repo . 您可以在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);
}
If you are using Prism
, than you should inherit your App
from PrismApplication
: 如果您使用
Prism
,则应该从PrismApplication
继承您的App
:
instead of public partial class App
should be public partial class App : PrismApplication
. 而不是
public partial class App
应该是public partial class App : PrismApplication
。
No need to declare INavigationService _navigationService;
无需声明
INavigationService _navigationService;
in your App
class, because it's already declared in PrismApplication
as NavigationService
property. 在您的
App
类中,因为它已在PrismApplication
声明为NavigationService
属性。
Prism
uses IUnityContainer
as Dependency Injection framework, which means for you, that all, that you will register in container: Prism
使用IUnityContainer
作为依赖注入框架,这对您来说意味着所有您将在容器中注册:
Container.RegisterType<ISomeService, SomeServiceImplementation>();
Unity container will automatically inject in constructor : Unity容器将自动注入构造函数 :
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.
Unity容器将实例化并注入实例。
PrismApplication
registers INavigationService
in a container, so, as I understand, you don't need to register your own instance and you could just add parameter of type INavigationService
in constructor of view model and unity container will inject instance of registered type. PrismApplication
在容器中注册INavigationService
,因此,据我所知,您不需要注册自己的实例,您可以在视图模型的构造函数中添加INavigationService
类型的参数,并且unity容器将注入已注册类型的实例。 The important thing, that you should give an exactly the navigationService name for your parameter, because Prism
"expects" such parameter name for navigation service. 重要的是,您应该为参数指定一个确切的navigationService名称,因为
Prism
“期望”导航服务的此类参数名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.