代码之家  ›  专栏  ›  技术社区  ›  Behzad

Prism Xamarin表单导航服务,DependencyService

  •  0
  • Behzad  · 技术社区  · 6 年前

    正如Prism所说,

    要在ViewModels中获得INavigationService,只需请求即可 作为构造函数参数

    https://prismlibrary.github.io/docs/xamarin-forms/Navigation-Service.html#getting-the-navigation-service

    像这样:

    public SpeakPageViewModel(INavigationService navigationService) : base(navigationService)
    
    {
    
    _navigationService = navigationService;
    
    }
    

    我想使用ITextToSpeech接口作为Prism示例:

    public MainPageViewModel(ITextToSpeech textToSpeech)
    {
        _textToSpeech = textToSpeech;
        SpeakCommand = new DelegateCommand(Speak);
    }
    

    https://prismlibrary.github.io/docs/xamarin-forms/Dependency-Service.html#use-the-service

    问题是 :向构造函数添加另一个参数时,导航不起作用。

    public SpeakPageViewModel(ITextToSpeech textToSpeech, INavigationService navigationService) : base(navigationService)
            {
                _navigationService = navigationService;
                _textToSpeech = textToSpeech;
            }
    

    项目文件: http://www.mediafire.com/file/nl6dx5c4mc3mg63/FirstPrismApp.rar

    1 回复  |  直到 6 年前
        1
  •  4
  •   Dan Siegel    6 年前

    Prism 7改变了这种行为,因为它实际上是一种依赖于辅助容器的反模式。您只需在 IPlatformInitializer 例如:

    public class iOSInitializer : IPlatformInitializer
    {
        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<ITextToSpeech, TextToSpeech_iOS>();
        }
    }