Xamarin Forms Prism:是否需要在构造函数中传递INavigationService?除了构造函数注入之外的任何其他方式

Xamarin Forms Prism:是否需要在构造函数中传递INavigationService?除了构造函数注入之外的任何其他方式,xamarin,xamarin.forms,prism,Xamarin,Xamarin.forms,Prism,对于xamarin表单中的prism框架,要从一个视图导航到另一个视图,是否必须在ViewModel的构造函数中实现构造函数注入并传递INavigationService 当我通过INavigationService时,我可以执行导航 public SomeViewModel(INavigationService navigationService) { _navigationService.NavigateAsync("SomeOtherPage"); } 但当我试图在需要的时候解决问题

对于xamarin表单中的prism框架,要从一个视图导航到另一个视图,是否必须在ViewModel的构造函数中实现构造函数注入并传递
INavigationService

当我通过INavigationService时,我可以执行导航

public SomeViewModel(INavigationService navigationService)
{
  _navigationService.NavigateAsync("SomeOtherPage");
}
但当我试图在需要的时候解决问题时,它就不起作用了

public SomeViewModel(INavigationService navigationService)
    {
      ContainerProvider.Resolve<T>();// ContainerProvider is IContainerProvider
    }
public someview模型(INavigationService导航服务)
{
ContainerProvider.Resolve();//ContainerProvider是IContainerProvider
}

除了在每个Viewmodel中注入构造函数之外,还有其他方法访问InActivationService吗?您所描述的是一种反模式,通常只是糟糕的设计。因为导航服务是一项非常特殊的服务,所以它也不起作用。它是专门为每个ViewModel创建的,因为导航服务必须具有所导航页面的上下文。如果没有它,它只能用于重置导航堆栈。试图在ViewModel中以任何其他方式解析导航服务可能会破坏MVVM设计模式

如果要使用导航服务,必须通过构造函数注入它。您也可以在Prism 7.1中简单地使用XAML导航。你可以看到一个样品与。你也可以在实践中看到这一点



将NavigationService与构造函数注入一起使用时,您是否遇到任何错误?此外,您不必手动解析NavigationService,它将通过构造函数注入来解析。Dan,感谢您的解释,这非常有帮助。我曾经在需要时使用解析器,而不是MvvmCross中的构造函数,所以我认为可能会有类似的情况。您能分享Xaml导航的示例/语法吗?我添加了一些基于Xaml的导航示例。请注意,这在100%的情况下是没有意义的,对于某些用例,它可以很好地工作。另外,请务必阅读文档以全面了解如何使用它,实际上有相当多的内置功能,包括确定何时可以导航或何时不能导航。谢谢Dan,我检查了您的示例。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xaml="clr-namespace:Prism.Navigation.Xaml;assembly=Prism.Forms"
             Title="{Binding Title}"
             x:Class="Prism.DI.Forms.Tests.Mocks.Views.XamlViewMockA">
    <Button x:Name="testButton" Command="{xaml:NavigateTo 'XamlViewMockB'}">
        <Button.CommandParameter>
            <xaml:NavigationParameters>
                <xaml:NavigationParameter Key="Foo" Value="Bar"/>
            </xaml:NavigationParameters>
        </Button.CommandParameter>
    </Button>
</ContentPage>