簡體   English   中英

在帶有棱鏡mvvm視圖模型的xamarin形式的xaml中包括xaml。 無法解析INavigationService

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

我想在所選頁面中包含一個后退按鈕。 由於某種原因,我將其稱為“面包屑”,請假定其名為“ 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();
    }
}

(*)

Microsoft.Practices.Unity.dll中發生類型為“ Microsoft.Practices.Unity.ResolutionFailedException”的異常,但未在用戶代碼中處理

附加信息:依賴關系的解析失敗,鍵入=“ Test.ViewModels.BreadcrumbControlViewModel”,名稱=“(none)”。

在以下期間發生異常:解析構造函數Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService navigationService)的參數“ navigationService”。

異常是:NullReferenceException-對象引用未設置為對象的實例。


在發生異常時,容器為:

解決Test.ViewModels.BreadcrumbControlViewModel,(無)

解析構造函數Test.ViewModels.BreadcrumbControlViewModel(Prism.Navigation.INavigationService navigationService)的參數“ navigationService”

App.cs容器/解析器注冊

ViewModelLocationProvider.Register<BreadcrumbControl, BreadcrumbControlViewModel>();

Container.RegisterTypeForNavigation<MainPage>();

如何在Xamarin Forms Prism MVVM應用程序中包括常見的后退按鈕樣式的東西(由視圖模型驅動)。

我看不到您完成的其他注冊的任何代碼。 您是否已正確注冊NavigationService? 在您的設置中,應該在注冊需要它的代碼之前在App.cs中注冊它。

另外,您應該使用ContainerControlledLifetimeManager 這實際上將其實現為單例(假設您希望在整個應用程序中傳遞相同的NavigationService)。 在這里解釋

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

由於Xamarin.Forms(基於頁面)中導航的性質,INavigationService僅適用於屬於Page類型的ViewModel。 它必須是一個頁面,否則,您將無法導航。 如果必須在此ContentView的VM中具有INavigationService,則必須在容器中注冊INavigationService,但它可能無法按預期運行,因為它將在Application.Current.MainPage而不是正確的Page上運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM