使用prism mvvm视图模型在xamarin形式的xaml中包含xaml。无法解决INavigationService

时间:2016-09-10 14:16:59

标签: c# mvvm xamarin.forms prism

我想在所选页面中包含一个后退按钮。出于某种原因,我已经把它叫做#34;面包屑",请假设它被称为" BackButton" :)

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

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

主页XAML - 注意本地: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();
    }
}

(*)

  

类型的例外   &#39; Microsoft.Practices.Unity.ResolutionFailedException&#39;发生在   Microsoft.Practices.Unity.dll但未在用户代码中处理

     

其他信息:依赖项的解析失败,type =   &#34; Test.ViewModels.BreadcrumbControlViewModel&#34;,name =&#34;(none)&#34;。

     

发生异常时:解析参数&#34; navigationService&#34;的   构造函数   Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService   的NavigationService)。

     

异常是:NullReferenceException - 未将对象引用设置为   对象的实例。

           

在例外时,容器是:

     

解析Test.ViewModels.BreadcrumbControlViewModel,(无)

     

解析参数&#34; navigationService&#34;构造函数   Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService   的NavigationService)

App.cs容器/解析器注册

ViewModelLocationProvider.Register<BreadcrumbControl, BreadcrumbControlViewModel>();

Container.RegisterTypeForNavigation<MainPage>();

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

2 个答案:

答案 0 :(得分:1)

我没有看到您完成的其他注册的任何代码。你有没有正确注册NavigationService?在您的设置中,您应该在注册需要它的代码之前在App.cs中注册它。

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

Dim s As Range, d As Range
Set d = Worksheets("Destination").Cells(6, 15) ' notice the column 15

For Each s in Worksheets("Source").Range("A5:A1500")
   If s = "a" Or s = "b" Then
      d(,3) = s(,3)
      d(,4) = s(,4)
      ' or d(,3).Resize(,19) = s(,3).Resize(,19) ' .Value optional
      Set d = d.Offset(1) ' move to next row
   End If
Next 

答案 1 :(得分:1)

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