简体   繁体   English

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

[英]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.

相关问题 Xamarin.Forms 和 Prism - 如何传递数据并导航到另一个视图? - Xamarin.Forms and Prism - How to pass data and navigate to another view? 如何在Xamarin.Forms中的Android特定页面和共享页面之间导航? - How to Navigate between Android Specific Pages and Shared Pages in Xamarin.Forms? 如何使用 Fresh.MVVM 在 Xamarin.Forms 中浏览模态页面 - How to navigate through modal pages in Xamarin.Forms using Fresh.MVVM 如何使用Prism / DryIoC在Xamarin.Forms中解决IValueConverter中的依赖关系 - How to resolve a dependency in IValueConverter in Xamarin.Forms using Prism/DryIoC Xamarin.Forms Prism-使用导航服务从一个详细信息页面导航到另一个 - Xamarin.Forms Prism - Using navigation service to navigate from one detail page to another 如何在页面xamarin.forms之间传递列表? - how to pass a list between pages xamarin.forms? 如何使用“ Xamarin.Forms”中的“ Prism.Forms”绑定视图和查看存在于不同dll中的模型? - How to bind views and view models that exist in different dlls using “Prism.Forms” in “Xamarin.Forms”? 使用Prism框架隐藏Xamarin.Forms中的选项卡 - Hiding a tab in Xamarin.Forms using Prism framework 如何使用Prism和Xamarin.Forms设置页面内容? - How do I set page content using Prism and Xamarin.Forms? 如何使用Prism在Xamarin.Forms中自动注册视图 - How to register views automatically in Xamarin.Forms with Prism
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM