xamarin 平台特定代码中的.NET MAUI依赖项注入

ddarikpa  于 2022-12-07  发布在  .NET
关注(0)|答案(1)|浏览(151)

有没有办法在特定平台的代码中注入服务,例如在MainActivity.cs中?我想Xamarin使用了DependencyService。还有,有没有可能将它们注入到后台ServicesBroadcast Receivers中?
我现在尝试做的是更新它们,但我想知道目前是否有更好的解决方案。据我所知,平台特定代码中的DI容器与您在MauiProgram.cs中使用的主容器不同,我看到过他们从头开始实现的示例。

3duebb1j

3duebb1j1#

我在Jame Montemagno's Youtube上找到了这个,我想这就是你要找的,视频是here

public static class ServiceHelper {

public static T GetService<T>() => Current.GetService<T>();

public static IServiceProvider Current =>
#if WINDOWS10_0_17763_0_OR_GREATER
    MauiWinUIApplication.Current.Services;
#elif ANDROID
    MauiApplication.Current.Services;
#elif IOS || MACCATALYST
    MauiUIApplicationDelegate.Current.Services;
#else
    null;
#endif }

相关问题