Skip to content

Instantly share code, notes, and snippets.

@jonathanMelly
Created February 28, 2024 11:23
Show Gist options
  • Save jonathanMelly/95a43f294015dc3560f253fcfc13f9f7 to your computer and use it in GitHub Desktop.
Save jonathanMelly/95a43f294015dc3560f253fcfc13f9f7 to your computer and use it in GitHub Desktop.
maui service provider
namespace WeatherTwentyOne;
courtesy of : https://github.com/davidortinau/WeatherTwentyOne/blob/main/src/WeatherTwentyOne/Services/ServiceExtensions.cs
public static class ServiceProvider
{
public static TService GetService<TService>()
=> Current.GetService<TService>();
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
}
@jonathanMelly
Copy link
Author

Or:
MauiProgram.Services.GetServices().First().CreateLogger("bob").LogDebug("hello debug");

  • Add REF
    var built = builder.Build();
    Services = built.Services;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment