-
I need to use conditional code in a few places. i.e. I thought this would be simple to do... there is lots of documentation on this but it seems there is a lot more to this than what is covered there. The current Avalonia templates create one "base" project where all of your code exists. I'll call this MyProject.UI. Then it creates platform-specific projects MyProject.UI.Desktop and MyProject.UI.Android, etc. Then I had to convert MyProject.UI.Desktop into something usable which is MyProject.UI.Windows and MyProject.UI.MacOS. Side-note, we should be using the MAUI structure and place all these per-platform projects in a Platforms directory. We also need to use single project for all platforms (except linux) at some point.... Anyway, this default setup simply doesn't support My original plan was to create a shared project will all the source code files. Using this How are other people solving this? I get the impression most have only used a single desktop csproj shared across mac/linux/windows and haven't encountered this quite yet. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Generally, different solutions are:
Note on #4, you still can build a projects reference tree that looks like graph TD;
A["MyProject.Platform (net8.0;net8.0-window)"] --> B["MyProject.UI (net8.0)"];
B --> C["MyProject.UI.Windows (net8.0-window)"];
B --> D["MyProject.UI.MacOS (net8.0-macos)"];
B --> E["MyProject.UI.Android (net8.0-android)"];
It's technically possible, since Avalonia 11.2 builds have some targets for that, but I would not recommend yet.
Not sure if I understood your solution, but it reminds me of Shared Projects and I don't like that. |
Beta Was this translation helpful? Give feedback.
-
Personally, I do this somewhat backwards using DI. Which is to say I still leave all the platform specific code in the platform specific projects and then inject it at runtime into the shared project. Most people forget that the "can't share platform specific code" restriction is only a compile time restriction. As such there is nothing stopping you from passing something that implements an interface from your shared code back to the shared code. This also avoids the hell of using preprocessor stuff (it's far more annoying that it seems). Also if you are feeling adventurous, in a lot of cases you can actually just reimplement a lot of the functionality yourself that the platform specific TFM implements without all the compile time restrictions (for desktops at least, it's as simple as |
Beta Was this translation helpful? Give feedback.
Generally, different solutions are:
#3
, but introduce a new platform specific yet shared project with multitargetting. Something what I would prefer, keeping UI in the single-target shared project, while still having…