r/csharp • • Apr 24 '24

Writing efficient platform specific Code in C# just got easier

The new Uno update introduces a way to integrate platform-specific code into a Uno Platform class library, and it does so remarkably well.

The new feature lets you write code that targets specific platforms within the same project. Here’s a quick example of how you can conditionally compile your application logic for different environments:

#if HAS_UNO
Console.WriteLine("Uno Platform - Pixel-perfect apps that run everywhere");
#else
Console.WriteLine("Windows - Built with Microsoft's own tooling");
#endif

 

#if __ANDROID__
Console.WriteLine("Android only code with net8.0-android");
#elif __IOS__
Console.WriteLine("iOS Only Code with net8.0-ios ");
#elif __WASM__
Console.WriteLine("WebAssembly Only Code with net8.0-browserwasm ");
#elif HAS_UNO_SKIA
Console.WriteLine("Skia Desktop Only Code with net8.0-desktop ");
#endif

0 Upvotes

11 comments sorted by

41

u/soundman32 Apr 24 '24

Struggling to know what is new here. Conditional compilation has been a thing since .net 1.0

4

u/Shnupaquia Apr 24 '24

I think it has to do with, starting from Uno Platform 5.2, in project or class libraries using the Uno.Sdk, a set of implicit file name conventions can be used to target specific platforms

9

u/Grasher134 Apr 24 '24

I'm pretty sure you could already do that with specific build targets...

5

u/dodexahedron Apr 24 '24

Yeah.

MSBuild can be intimidating to approach, for sure, but stuff like this is definitely easy and hella cleaner to do in the csproj or a Directory.build.props or something by conditional inclusion/exclusion of well-organized files, based on defined constants and selected targets. I strongly dislike heavy use of #if in code files, especially if it is used for things like changing one keyword in a type or member declaration.

More generally, though, Uno is quite a lot more than just that.

Puzzling why this "feature" was considered newsworthy, though, outside of just being a pretty blatant plug with a thin veneer of being "release notes."

3

u/Larkonath Apr 25 '24

Sorry to hijack but what do people think of Uno?

Maui seems way too buggy to be relied on and Avalonia doesn't seem really good at mobile (I used it successfully on desktop though).

9

u/PleasantGanache Apr 24 '24

Can we stop with the UNO advertisements? It's getting boring

2

u/Slypenslyde Apr 25 '24

I feel this way about ASP .NET Core but I think we're both going to be unhappy.

1

u/Shnupaquia Apr 24 '24

They just had their biggest release to date...

Also, you can always you know, check out the many other threads that are not Uno if it bothers you so much.

5

u/Slypenslyde Apr 24 '24

This is the default MAUI style and in some ways I miss the multi-project style. Some stuff feels better segregated to platform-specific files, other stuff feels better with conditional compilation. I don't think there's a "winner", just a need for both.

-2

u/TuberTuggerTTV Apr 24 '24

I'll be trying this out.

Hopefully it's easy to migrate over from other UI frameworks.