In pre 4.0 Uno-Platform, I was able to use the #if __WASM__
compiler directive for WebAssembly specific code in a cross platform class library. This was very useful for creating Wasm-specific widgets using the Wasm specific SetHtmlAttribute
method.
However, in version 4.0 Uno-Platform, my code isn't building because the compiler isn't finding the __WASM__
constant. I hesitate adding it manually to the libraries *.csproj file ...
<DefineConstants Condition="$(TargetFramework.StartsWith('netstandard'))">__WASM__;$(DefineConstants);</DefineConstants>
... because I'm afraid it will build if the target is WPF. So, is there a better approach?
While your approach covers the scenario where a given API is available on all Uno targets, it will not work in case it really is Wasm or Skia specific, unfortunately. For those cases though we have another type of library - cross-runtime library (see docs). Currently this kind of library can only be created via dotnet
command-line tooling:
dotnet new unolib-crossruntime -o MyCrossRuntimeLibrary
By default it generates the following solution layout:
Both projects are pointing to the same source-code files, however the ".Wasm" one is specifically referencing the WebAssembly runtime and will correctly adhere to #if __WASM__
declarations. Similarly you could create another project for ".Skia" to have Skia-specific declarations.