簡體   English   中英

Xamarin的Prism,INavigationService的依賴項失敗

[英]Prism for Xamarin, dependency failed for INavigationService

我正在使用PRISM開發適用於iOS的示例Xamarin.Forms應用程序。

但是,當我嘗試在ViewModel中使用INavigationService時,失敗了,說接口INavigationService的依賴項失敗

我不知道我在做什么錯,我們如何解決這個問題,或者這是INavigationServices注入中的錯誤。

感謝Divikiran Ravela

繼承人代碼

    public App()
    {
        // The root page of your application
        Prism = new PrismBootStrap();
        Prism.Run(this);
    }

    public class PrismBootStrap : UnityBootstrapper
{
    protected override Page CreateMainPage()
    {
        return new NavigationPage(Container.Resolve<HomePage>());
    }

    protected override void RegisterTypes()
    {
        Container.RegisterTypeForNavigation<HomePage, HomePageViewModel>();
    }
}


    public class HomePage : ContentPage
    {

        public HomePageViewModel ViewModel { get; set; }


   public HomePage()
    {
        ViewModel = App.Prism.Container.Resolve<HomePageViewModel>();
        Content = new StackLayout
        {
            Children = {
                new Label { Text = "Hello ContentPage" }
            }
        };
    }
}


public class HomePageViewModel : BindableBase
{
    private readonly INavigationService _navigationService;

    public HomePageViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService; //Fails here
    }
}

INavigationService取決於ViewModelLocator的使用。 如果您不使用它,那么該服務將無法注入。 可以使用ViewModelLocator.AutowireViewModel附加屬性代替手動解析VM。 此外,您不想那樣使用您的容器。 使用DI時只需要一個合成根。

另外,我建議使用最新的6.1預覽版。 它具有大量的導航改進。

暫無
暫無

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

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