Prism Xamarin Forms导航服务DependencyService [英] Prism Xamarin Forms Navigation service, DependencyService
问题描述
正如棱镜所说,
要在您的ViewModels中获得INavigationService,只需索要它 作为构造函数参数
To obtain the INavigationService in your ViewModels simply ask for it as a constructor parameter
像这样:
public SpeakPageViewModel(INavigationService navigationService) : base(navigationService)
{
_navigationService = navigationService;
}
我想将ITextToSpeech接口用作Prism示例:
and I want to use ITextToSpeech interface as Prism sample :
public MainPageViewModel(ITextToSpeech textToSpeech)
{
_textToSpeech = textToSpeech;
SpeakCommand = new DelegateCommand(Speak);
}
https://prismlibrary.github .io/docs/xamarin-forms/Dependency-Service.html#use-the-service
问题是:在向构造函数添加另一个参数时,导航不起作用.
The problem is: when add another parameter to the constructor, the navigation doesn't work.
public SpeakPageViewModel(ITextToSpeech textToSpeech, INavigationService navigationService) : base(navigationService)
{
_navigationService = navigationService;
_textToSpeech = textToSpeech;
}
项目文件: http://www.mediafire.com/file/nl6dx5c4mc3mg63/FirstPrismApp.rar
推荐答案
Prism 7更改了此行为,因为它实际上是依赖于辅助容器的反模式.您只需要在IPlatformInitializer
中注册您的TextToSpeech服务,例如:
Prism 7 changed this behavior as it is actually an anti-pattern to rely on a secondary container. You simply need to register your TextToSpeech service in the IPlatformInitializer
like:
public class iOSInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<ITextToSpeech, TextToSpeech_iOS>();
}
}
这篇关于Prism Xamarin Forms导航服务DependencyService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!