unoplatform / uno

Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
https://platform.uno
Apache License 2.0
8.83k stars 710 forks source link

Conditional WASM/Skia `xmlns` doesn't work inside of a library #5372

Open kazo0 opened 3 years ago

kazo0 commented 3 years ago

Creating an issue on GitHub to track this issue

Current behavior

Using conditional XAML with the wasm: or skia: prefix does not work when compiling inside of an Uno Library

How to reproduce it (as minimally and precisely as possible)

  1. Create a new Uno Library
  2. Add the following code:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:wasm="http://uno.ui/wasm"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    mc:Ignorable="d wasm">
    <wasm:Style x:Key="MaterialToggleSwitchStyle"
                TargetType="ToggleSwitch">
                  ....
    </wasm:Style>
</ResourceDictionary>
  1. Notice that the Style is not created in the generated code

Workaround

Use a Cross-Runtime Library?

Environment

Nuget Package:

Nuget Package Version(s):

Affected platform(s):

IDE:

Relevant plugins:

Xiaoy312 commented 3 years ago

But, possibly we could adjust the project to generate multiple netstandard packages? ala Uno.UI with Uno.UI.WebAssembly & Uno.UI.Skia An other option could be to have runtime conditional xaml?: xaml_uri_or_using_namespace?is_platform(platform0[,platform1...])

jeromelaban commented 3 years ago

There's already support for this using Cross-Runtime libraries.

baskren commented 2 years ago

For reference: https://stackoverflow.com/questions/70994056/uno-platform-cross-platform-library-if-conditional-for-wasm

Stack Overflow
Uno-Platform: Cross Platform Library, #if conditional for WASM?
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 widg...
MartinZikmund commented 1 year ago

@kazo0 I think this can now be closed, as the docs were updated, right?

jeromelaban commented 1 year ago

This may actually change with net8.0-browser, we'll need to reevaluate at that time.

MartinZikmund commented 10 months ago

@jeromelaban Darsh is now doing https://github.com/unoplatform/Uno.Themes/issues/1298, what should we do for wasm: and skia: prefixes in the docs? Should we explicitly mention it works in cross-runtime libraries only? Plus, should we have a crossruntime: prefix?

MartinZikmund commented 10 months ago

The problem gets worse because if someone now uses not_wasm or not_skia in non cross-runtime library, it is evaluated to true, which is very confusing. Maybe we should make the compilation fail unless the library is cross-runtime library and wasm or skia prefixes are used OR we just make wasm==skia==crossruntime in non-cross runtime libraries, but still - doing not_skia would the also mean not_wasm, so the compiler error would make more sense to me. It could even mention something like "if you want to use skia, wasm, not_skia and not_wasm, you need to use cross-runtime library" with a link to the docs. @jeromelaban @Youssef1313

jeromelaban commented 10 months ago

@MartinZikmund this scenario is particularly broken now that net8.0-browser is not slated to be included in .NET. Failing the compilation is likely to be an important breaking change.

At this time, the easiest way to handle this particular scenario is to make custom user controls that switch using runtime conditions (by detecting wasm), or to use state triggers to switch values.

The more complex way is to use cross-runtime libraries.