在带有棱镜 mvvm 视图模型的 xamarin 表单上将 xaml 包含在 xaml 中。无法解析 INavigationService

include xaml in xaml on xamarin forms with prism mvvm view model. Cannot resolve INavigationService

我想在所选页面中包含一个后退按钮。出于某种原因,我将其命名为 "Breadcrumb",请假定其名称为 "BackButton" :)。

问题是导航服务没有在 ioc 中拉通。

下面是我遇到的例外代码:

主页XAML - 注意local:BreadcrumbControl

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="Test.Views.MainPage"
             xmlns:local="clr-namespace:Test.Views">

    <StackLayout>
        <local:BreadcrumbControl x:Name="Breadcrumb" />
    </Stacklayout>
<ContentPage>

面包屑控件XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
             prism:ViewModelLocator.AutowireViewModel="True"
             x:Class="Test.Views.BreadcrumbControl">

    <Button Text="Back" Command="{Binding NavigateCommand}" CommandParameter="NavigationPage/ManageFoodGroupsPage" />

</ContentView>

面包屑页面视图模型

public class BreadcrumbControlViewModel : BindableBase
{
    INavigationService navigationService;

    public DelegateCommand<string> NavigateCommand { get; set; }

    // If i uncomment the navigationservice the following error occurs (*)
    public BreadcrumbControlViewModel(/*INavigationService navigationService*/)
    {
        this.navigationService = navigationService;
        NavigateCommand = new DelegateCommand<string>(Navigate);
    }

    private void Navigate(string name)
    {
        navigationService.GoBackAsync();
    }
}

(*)

An exception of type 'Microsoft.Practices.Unity.ResolutionFailedException' occurred in Microsoft.Practices.Unity.dll but was not handled in user code

Additional information: Resolution of the dependency failed, type = "Test.ViewModels.BreadcrumbControlViewModel", name = "(none)".

Exception occurred while: Resolving parameter "navigationService" of constructor Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService navigationService).

Exception is: NullReferenceException - Object reference not set to an instance of an object.


At the time of the exception, the container was:

Resolving Test.ViewModels.BreadcrumbControlViewModel,(none)

Resolving parameter "navigationService" of constructor Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService navigationService)

App.csContainer/Resolver报名

ViewModelLocationProvider.Register<BreadcrumbControl, BreadcrumbControlViewModel>();

Container.RegisterTypeForNavigation<MainPage>();

我如何在 Xamarin Forms Prism MVVM 应用程序中包含一个常见的后退按钮样式的东西(由视图模型驱动)。

我没有看到您完成的其他注册的任何代码。您是否已正确注册 NavigationService?在您的设置中,您应该先在 App.cs 中注册它,然后再注册需要它的代码。

此外,您应该使用 ContainerControlledLifetimeManager。这实质上将其实现为单例(假设您希望在整个应用程序中传递相同的 NavigationService)。这是解释here

Container.RegisterType<NavigationService>(new ContainerControlledLifetimeManager());

由于 Xamarin.Forms 中导航的性质(基于页面),INavigationService 仅适用于属于页面类型的 ViewModel。它必须是一个页面,否则,您将无法导航。如果您必须在此 ContentView 的 VM 中拥有 INavigationService,那么您必须向容器注册 INavigationService,但它的行为可能不会像您预期的那样,因为它将在 Application.Current.MainPage,而不是正确的页面上工作。