Xamarin的Prism,INavigationService的依赖失败

时间:2016-01-05 23:24:36

标签: c# ios xamarin prism

我正在使用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
    }
}

1 个答案:

答案 0 :(得分:1)

INavigationService取决于ViewModelLocator的使用。如果您不使用它,则无法注入该服务。而不是手动解析您的VM,使用ViewModelLocator.AutowireViewModel附加属性。此外,你不想这样使用你的容器。使用DI时,您只需要一个组合根。

另外,我建议使用最新的6.1预览版。它有很多导航改进。