I'm working with VS 17.11, and MAUI 8.
There are a lot of articles about "dependency injection", including on a platform basis.
But something like this seems possible too:
#if IOS
string platformName = "This is iOS";
#elif WINDOWS
string platformName = "This is Windows";
#elif ANDROID
string platformName = "This is Android";
#else
string platformName = "This is UNKNOWN platform";
#endif
But, how does this really work? I was under the impression that the MAUI project would build the shared code (as a library?), then link in the platform-specific code, all in "Common Intermediate Language". But, having this mechanism, it seems like each platform will get built separately.
Am I missing something? If this is a reasonable way to do things, is there an advantge to using dependency-injection for something simple like this?
First of all, you can check the official document about Conditional compilation. The #if <Platform>
is preprocessor directive.
The C# compiler compiles the code between the #if directive and #endif directive only if the specified symbol is defined, or not defined when the ! not operator is used.
The Maui project (typically) builds separate assemblies for each platform so you can use the #if <Platform>
.
And the official document about Dependency injection in Maui doesn't mention the platform code. The dependency injection almost uses in the maui share library not platform part. But it is necessary for the old Xamarin pattern (split the project into platform specific and platform agnostic modules).