简体   繁体   English

如何在 UNO 平台应用程序中使用 Microsoft.Extensions.DependencyInjection

[英]How can I use Microsoft.Extensions.DependencyInjection in an UNO Platform app

I am new to UNO Platform and I am trying to develop my first application with this framework.我是UNO 平台的新手,我正在尝试使用此框架开发我的第一个应用程序。 I would like to use a DI toolkit also so I chose Microsoft.Extensions.DependencyInjection that should be compliant with this Platform.我也想使用 DI 工具包,所以我选择了应该与这个平台兼容的Microsoft.Extensions.DependencyInjection

By the way, I cannot understand how can I inject dependencies to my ViewModels.顺便说一句,我无法理解如何向我的 ViewModel 注入依赖项。 I red this post: How can I use Microsoft.Extensions.DependencyInjection in an .NET Core console app?我红了这篇文章: 如何在 .NET Core 控制台应用程序中使用 Microsoft.Extensions.DependencyInjection? and this is my approach:这是我的方法:

App.xaml.cs :应用程序.xaml.cs

    public App()
    {
        ConfigureFilters(global::Uno.Extensions.LogExtensionPoint.AmbientLoggerFactory);

        var services = new ServiceCollection()
            .AddSingleton<IMessageBroker, MessageBroker>();
        var serviceProvider = services.BuildServiceProvider();

        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

where IMessageBroker and MessageBroker are the interface and the implementation of my class I would like to handle with DI.其中IMessageBrokerMessageBroker是我想用 DI 处理的类的接口和实现。 Then on my ViewModel I do:然后在我的 ViewModel 我做:

MainPageViewModel.cs MainPageViewModel.cs

public MainPageViewModel(IMessageBroker broker)
{
     // some code
}

So far, so good.到现在为止还挺好。

For MVVM toolkit, I chose MVVM helpers .对于 MVVM 工具包,我选择了MVVM helpers If I understood correctly, this MVVM toolkit does not provide any convention to attach View to ViewModel so I should do it manually:如果我理解正确的话,这个 MVVM 工具包没有提供任何将 View 附加到 ViewModel 的约定,所以我应该手动完成:

MainPage.xaml.cs主页.xaml.cs

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        // How can I get a reference to ServiceProvider for calling GetService
        var broker = serviceProvider.GetService<IMessageBroker>()

        this.DataContext = new MainPageViewModel(broker);
    }
}

How can I get a reference here to ServiceProvider for calling GetService<IMessageBroker>() ?如何在此处获得ServiceProvider的引用以调用GetService<IMessageBroker>()

Any hint would be highly appreciated.任何提示将不胜感激。

one way you could do is having a static class ServiceLocator, which is initialized only from the beginning.你可以做的一种方法是拥有一个静态类 ServiceLocator,它只从一开始就初始化。 This class exposes a readonly ServiceProvider, and you can call it to get the thing you register.这个类公开了一个只读的ServiceProvider,你可以调用它来获取你注册的东西。

You may read somewhere else, or someone else here may comment this is anti-pattern , which is generally out of context.你可以在别处阅读,或者这里的其他人可能会评论这是anti-pattern ,这通常是断章取义的。 Depending on what you are trying to do and how your solution is structured, this service locator pattern can be totally fine.根据您尝试执行的操作以及解决方案的结构,此服务定位器模式可能完全没问题。

here is a sample project you can use for reference https://github.com/weitzhandler/UnoRx/tree/f2a0771e6a513863108e58ac7087078f39f7e3ed这是一个示例项目,您可以参考https://github.com/weitzhandler/UnoRx/tree/f2a0771e6a513863108e58ac7087078f39f7e3ed

an alternative will be, writing a viewmodel locator, which automatically resolves all dependencies through your view.另一种方法是编写一个视图模型定位器,它会通过您的视图自动解析所有依赖项。 This, however, may or may not overkill your use case.但是,这可能会或可能不会过度使用您的用例。

finally, my personal flavor, having a shell project that depends on auto discovery and auto resolving;最后,我的个人风格,有一个依赖于自动发现和自动解析的 shell 项目; and each discovered smaller projects will internally depends on either service locator or function compositions每个发现的较小项目将在内部取决于服务定位器或功能组合

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 .NET Core 控制台应用程序中使用 Microsoft.Extensions.DependencyInjection? - How can I use Microsoft.Extensions.DependencyInjection in an .NET Core console app? 如何在 Microsoft.Extensions.DependencyInjection 中注册类型? - How to register types in Microsoft.Extensions.DependencyInjection? Autofac - 我可以使用 Microsoft.Extensions.DependencyInjection 的注册样式而不是 Autofac 吗? - Autofac - Can I use Microsoft.Extensions.DependencyInjection's Registering Style instead of Autofac one? 使用 Microsoft.Extensions.DependencyInjection,我能否在提供额外的构造函数参数的同时解析类型并构造实例? - With Microsoft.Extensions.DependencyInjection, can I resolve the type and construct an instance while providing extra constructor parameters? 如何在 Microsoft.Extensions.DependencyInjection 中注册现有实例? - How to register existing instances in Microsoft.Extensions.DependencyInjection? 如何使用 Microsoft.Extensions.DependencyInjection 在 .net 框架中的 webapi 中注入依赖项? - How do I inject dependency in webapi in .net framework using Microsoft.Extensions.DependencyInjection? class 库中的 Microsoft.Extensions.DependencyInjection - Microsoft.Extensions.DependencyInjection in a class library Microsoft.Extensions.DependencyInjection 是使用 singleton 概念吗? - Is Microsoft.Extensions.DependencyInjection using singleton concept? 是否可以在同一个 class 中使用,同一接口的不同实现,使用 Microsoft.Extensions.DependencyInjection 中的服务 - Is it possible to use in the same class, different implementation of the same interface, using services in Microsoft.Extensions.DependencyInjection 带有Microsoft.Extensions.DependencyInjection的.NET Framework类库 - .NET Framework Class Library with Microsoft.Extensions.DependencyInjection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM