使用模板10进行依赖注入 [英] Dependency Injection using Template 10

查看:75
本文介绍了使用模板10进行依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些代码从我使用Prism/Unity开发的旧Windows 8.1应用程序迁移到使用Template 10和Unity的新UWP应用程序.我在模板10的文档中看到了此处您可以override ResolveForPage方法.

I am trying to migrate some code from an old Windows 8.1 app that I had developed using Prism/Unity to a new UWP app using Template 10 and Unity. I have seen in the documentation for Template 10 here that you can override the ResolveForPage method.

在旧的Windows 8.1应用程序中,Prism中有一个Resolve方法,我会这样override:

In my old Windows 8.1 app, there is a Resolve method in Prism that I would override like this:

protected override object Resolve(Type type)
{
    return Container.Resolve(type);
}

Template 10方法的签名为

The signature for the Template 10 method is

public override INavigable ResolveForPage(Page page, NavigationService navigationService)

所以我不确定如何转换.我已经在App.xaml.csOnInitializeAsync中注册了我的存储库,如下所示:

so I am not exactly sure how to convert this. I have registered my repository in OnInitializeAsync in my App.xaml.cs, like so:

Container.RegisterType<IPayeesRepository, PayeesRepository>(new ContainerControlledLifetimeManager());

其中ContainerUnityContainer实例.我的问题是,当我尝试在另一页上注入依赖项时,得到了NullReferenceException,因为_payeesRepositorynull.在我看来,没有调用依赖项注入的构造函数,并且如果我删除默认的构造函数,则会收到错误消息.有没有人让Unity可以与Template 10一起使用,可能对我可能缺少的建议有什么建议?

Where Container is a UnityContainer instance. My problem is that when I try to inject the dependency on another page, I get a NullReferenceException because _payeesRepository is null. It seems to me like the constructor with the dependency injection is not being called, and if I remove the default constructor then I get an error. Has anyone gotten Unity to work with Template 10 that may have any suggestions what I may be missing?

我也尝试使用Dependency属性,如下所示:

I also tried using the Dependency attribute like so:

[Dependency]
private IPayeesRepository _payeesRepository { get; set; }

但这也不起作用.看来IPayeesRepository只是没有被实例化,但我不确定.在我的Windows 8.1应用程序中,从未显式实例化它,因此我觉得它与不重写Resolve方法有关.

But that doesn't work either. It seems like the IPayeesRepository is just not being instantiated, but I'm not exactly sure. In my Windows 8.1 app, it is never explicitly instantiated, so I have a feeling it has something to do with not overriding the Resolve method.

推荐答案

我做到了(但就我而言,我还有另一个烦人的问题,我将在稍后提及,也许还会有类似的问题).

I made it work (but In my case I'm having another anoying issue I'll mention later and probably in a SO quiestion too).

一方面, 对此问题的回答太多指导我使用ViewModel的DI解决此问题.

On the one hand, the Ask Too Much's answer to this question guided me to solve this problem with ViewModel's DI.

在App.xaml.cs中:

In App.xaml.cs:

public override async Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
{
    // long-running startup tasks go here
    AppController.Initialize();
    await Task.CompletedTask;
}

AppController是我配置应用程序(包括容器)的地方.

AppController is the place where I configure the app, including the container.

下一步,在App.xaml.cs中:

next, in App.xaml.cs:

public override INavigable ResolveForPage(Page page, NavigationService navigationService)
{
    if (page is MainPage)
    {
        return SimpleIoc.Default.GetInstance<MainPageViewModel>();
        //(AppController.UnityContainer as UnityContainer).Resolve<INavigable>();
    }
    else
        return base.ResolveForPage(page, navigationService);
}

但是您还必须:

从Page XAML中删除<Page.DataContext>. 从page.xaml.cs中删除构造函数,我的MainPage.xaml.cs就是这样

Remove <Page.DataContext> from the Page XAML. Remove constructors from page.xaml.cs, my MainPage.xaml.cs is like this

public sealed partial class MainPage : Page
{
    MainPageViewModel _viewModel;

    public MainPageViewModel ViewModel
    {
        get { return _viewModel ?? (_viewModel = (MainPageViewModel)DataContext); }
    }    
}

在虚拟机上注入依赖项:

Inject your dependencies on your VM:

public MainPageViewModel(IShapeService shapeService)
{     
   // this is just a POC            
}

仅此而已,它应该对您有用.

And that's all, it should work for you.

我使用以下相同信息更新了Wiki :一会儿...另外,请让我知道我使其与Unity和MVVMLight.SimpleIoC一起使用,结果相同,由于IShapeService实际上是PCL库中的WCF代理,因此出现了System.PlatformNotSupportedException.因为我只是意识到UWP不支持配置文件,所以必须重构(大声笑!)

I updated the wiki with this same information in a while... Also, just let you know that I made it to work with Unity and with MVVMLight.SimpleIoC with same result, a System.PlatformNotSupportedException due to what IShapeService really is is a WCF proxy that is in a PCL library that I'll have to refactor because I just realize that UWP doesn't support config files (lol!)

我希望它能帮助您并节省时间.

I hope it helps and save you time.

这篇关于使用模板10进行依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆