第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將 xamarin 表單與 IServiceProvider 一起使用

將 xamarin 表單與 IServiceProvider 一起使用

C#
侃侃爾雅 2021-09-19 16:02:14
我正在研究 xamarin 形式的&ldquo;依賴注入&rdquo;,并發(fā)現(xiàn)了一些使用類似ContainerBuilder.?像這樣在網(wǎng)上找到的解決方案討論了如何設(shè)置 DI 并將它們注入到您的視圖模型中。然而,就我個(gè)人而言,由于幾個(gè)原因,我沒(méi)有發(fā)現(xiàn)這個(gè)或視圖模型和綁定的整個(gè)概念非常整潔。我寧愿創(chuàng)建可以由業(yè)務(wù)邏輯重用的服務(wù),這似乎使代碼更清晰。我覺(jué)得實(shí)施 anIServiceProvider會(huì)導(dǎo)致更清晰的實(shí)施。我計(jì)劃實(shí)施這樣的服務(wù)提供者:IServiceProvider?Provider?=?new?ServiceCollection() ????????????????????????????.AddSingleton<OtherClass>() ????????????????????????????.AddSingleton<MyClass>() ????????????????????????????.BuildServiceProvider();首先,我不確定為什么沒(méi)有這些的 xamarin 示例。所以,我不確定朝著這個(gè)方向前進(jìn)是否有什么問(wèn)題。我已經(jīng)調(diào)查過(guò)了ServiceCollection。它來(lái)自的軟件包Microsoft.Extensions.DependencyInjection名稱中沒(méi)有&ldquo;aspnetcore&rdquo;。但是,它的所有者是&ldquo;aspnet&rdquo;。我不完全確定是否ServiceCollection僅適用于 Web 應(yīng)用程序,或者將其用于移動(dòng)應(yīng)用程序是否有意義。它是安全使用IServiceProvider與ServiceCollection只要我使用所有的單身?是否有任何問(wèn)題(在性能或內(nèi)存方面)我失蹤了?更新注意到了一些事情:文檔鏈接的日期與Microsoft.Extensions.DependencyInjection仍處于測(cè)試階段的時(shí)間大致相同文檔中&ldquo;使用依賴注入容器的幾個(gè)優(yōu)點(diǎn)&rdquo;列表中的所有要點(diǎn)也適用于DependencyInjection我所看到的。Autofac?過(guò)程似乎圍繞著我試圖避免使用的 ViewModel。更新 2在導(dǎo)航功能的幫助下,我設(shè)法將 DI 直接放入頁(yè)面的后臺(tái)代碼中,如下所示:public static async Task<TPage> NavigateAsync<TPage>()? ? where TPage : Page{? ? var scope = Provider.CreateScope();? ? var scopeProvider = scope.ServiceProvider;? ? var page = scopeProvider.GetService<TPage>();? ? if (navigation != null) await navigation.PushAsync(page);? ? return page;}
查看完整描述

2 回答

?
飲歌長(zhǎng)嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

此實(shí)現(xiàn)使用Splat和一些幫助器/包裝器類來(lái)方便地訪問(wèn)容器。


服務(wù)的注冊(cè)方式有點(diǎn)冗長(zhǎng),但它可以涵蓋我迄今為止遇到的所有用例;并且生命周期也可以很容易地改變,例如切換到服務(wù)的懶惰創(chuàng)建。


只需使用ServiceProvider類從代碼中的任何位置的 IoC 容器中檢索任何實(shí)例。


注冊(cè)您的服務(wù)


public partial class App : Application

{

    public App()

    {

        InitializeComponent();

        SetupBootstrapper(Locator.CurrentMutable);

        MainPage = new MainPage();

    }


    private void SetupBootstrapper(IMutableDependencyResolver resolver)

    {

        resolver.RegisterConstant(new Service(), typeof(IService));

        resolver.RegisterLazySingleton(() => new LazyService(), typeof(ILazyService));

        resolver.RegisterLazySingleton(() => new LazyServiceWithDI(

            ServiceProvider.Get<IService>()), typeof(ILazyServiceWithDI));

        // and so on ....

    }

ServiceProvider的使用


// get a new service instance with every call

var brandNewService = ServiceProvider.Get<IService>();


// get a deferred created singleton

var sameOldService = ServiceProvider.Get<ILazyService>();


// get a service which uses DI in its contructor

var another service = ServiceProvider.Get<ILazyServiceWithDI>();

ServiceProvider的實(shí)現(xiàn)


public static class ServiceProvider

{

    public static T Get<T>(string contract = null)

    {

        T service = Locator.Current.GetService<T>(contract);

        if (service == null) throw new Exception($"IoC returned null for type '{typeof(T).Name}'.");

        return service;

    }


    public static IEnumerable<T> GetAll<T>(string contract = null)

    {

        bool IsEmpty(IEnumerable<T> collection)

        {

            return collection is null || !collection.Any();

        }


        IEnumerable<T> services = Locator.Current.GetServices<T>(contract).ToList();

        if (IsEmpty(services)) throw new Exception($"IoC returned null or empty collection for type '{typeof(T).Name}'.");


        return services;

    }

}

這是我的 csproj 文件。沒(méi)什么特別的,我添加的唯一 nuget 包是 Spat


共享項(xiàng)目 csproj


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>

    <TargetFramework>netstandard2.0</TargetFramework>

    <ProduceReferenceAssembly>true</ProduceReferenceAssembly>

  </PropertyGroup>


  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

    <DebugType>portable</DebugType>

    <DebugSymbols>true</DebugSymbols>

  </PropertyGroup>


  <ItemGroup>

    <PackageReference Include="Splat" Version="9.3.11" />

    <PackageReference Include="Xamarin.Forms" Version="4.3.0.908675" />

    <PackageReference Include="Xamarin.Essentials" Version="1.3.1" />

  </ItemGroup>

</Project>


查看完整回答
反對(duì) 回復(fù) 2021-09-19
  • 2 回答
  • 0 關(guān)注
  • 173 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)