Platform-Specific Code in .NET MAUI Using Dependency Injection

Gerald Versluis · 13K views · 2021 · 13:17

Open on YouTube

Software architecture & designSoftware (other)tutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering .NET MAUI questions about calling Android or iOS APIs, such as BLE, image conversion, or platform services, from shared code.

Recommended by 1 people across 1 different months, 2022 to 2023.

What it is

A 13-minute tutorial by Gerald Versluis showing how to write platform-specific code in .NET MAUI using dependency injection. He defines a shared interface, implements it for Android and iOS, registers the implementations in the DI container, and compares this to the multi-targeting approach from an earlier video.

What people said

Recommends summer · 2023-04 · Stack Overflow · on “Cannot reference platform-specific services in .NET MAUI project”

About reference platform-specific services in .NET MAUI project: Way1 : You can refer to official doc: Invoke platform code [LINK] . It also provides code sample: InvokePlatformCodeDemos [LINK] . Way2 : I searched for information about this and found this video by Gerald Versluis: Platform-Specific Code in .NET MAUI Using Dependency Injection [this video] . The solution he provides is concise and clear. MauiAppBuilder.Services.AddTransient<TService, TImplementation>(IServiceCollection) And it puts services on a specific platform under the same namespace: namespace yourProjectName.Platforms

Cites Adrain · 2022-05 · Stack Overflow · on “How to use BLE in Maui?”

As you can see here [LINK] : BLE is not on our roadmap for .NET 6. We are discussing bringing Essentials together with Forms in dotnet/maui as there's a growing overlap and interdependence. If you want to use BLE, you may need to write platform-specific code, you can refer to this [this video] to know how to use .NET MAUI Dependency Injection.

Cites Adrain · 2022-05 · Stack Overflow · on “Convert MAUI ImageSource to png”

You can use Dependency Injection [this video] to deal with it, implement the interface on the platform you want. For example, I did a test on android: The interface: public interface MyService { public void Convert( string filename, ImageSource img); } } The implemention: public class AndroidService : MyService { public async void Convert(string filename, ImageSource img) { System.IO.Stream outputStream = null; var handler = new FileImageSourceHandler(); Bitmap pic = await handler.LoadImageAsync(img, Android.App.Application.Context); var savedImageFilename = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), filename); bool success = false; using (ou

Cites Dumber_Texan2 · 2022-09 · Stack Overflow · on “The type or namespace name 'Platforms' does not exist in the namespace MyApp”

@Jason Thanks for the comment! I'm not sure what that means. I've followed this video. Seems a namespace like MyApp.Platforms should resolve. Am I missing something? [this video]