-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#elif WINDOWS doesn't work #6815
Comments
In my experience, the underscore versions of the define's are never used. Only the "text" defines, such as Can you show a code sample that doesn't work if you use only the "text" defines? |
For example, with this code: #if WINDOWS
// Windows
#elif ANDROID
// Android
#elif IOS
// iOS
#elif MACCATALYST
// MacCatalyst
#endif I get this result, which is what I expected (note that the IOS platform is selected, and the iOS comment is "green", meaning it is included in the compilation, and all others are "grayed out"): |
We're using this in various places in our own code and is described in the docs here as being I don't think there is an issue here. Have a look in our codebase here to see how we're using it to guide you as an example. |
Hi @Eilon thank you for your time. In this thread on the maui documentation, it seems we have to use If I set |
@jfversluis you may missed the following part Next, #elif WINDOWS is recognized by Visual Studio but not by the compiler as it tells me it can't find the implementation of my service. There is a bug. Don't close this yet please |
As I said: we have this in multiple places in our codebase, it works fine there. If it says that there is still something wrong, I would think it is something else. It would greatly help if you provide a reproduction sample that shows this behavior or at the very least specify what "tells me it can't find the implementation of my service." is exactly. What is the exact error message and the code that triggers it? |
This seems to work fine for me: https://github.com/jfversluis/Repro6815 Just have to make sure that either all platforms have the |
It builds on your repro but I can't launch it. YES ! I found the bug. @jfversluis In your repo, create a folder Services inside Repro6815\Platforms\Windows Move your windows implementation inside : Repro6815\Platforms\Windows\Services\SomeService.cs Now it doesn't build anymore even if you change the namespace |
That indeed seems weird! Not to be rude here or anything just want to point it out, but I feel this would have all saved us a lot of time if there was a reproduction attached in the first place. That's why we ask for it. It can be the subtle differences. |
Yeah I know but trust me those last few days I've been submitting severals bugs (you saw some of them) I do what I can to help the community but i find several bugs a day on MAUI (XAML crash or weird rendering compared to the same code on Xamarin Forms) so if I spend 1 or 2 hour by bug to create the project, the repro step, to publish on github, to fill the issue here, I bet my boss would ask me to stop reporting new issues with something stupid like "other people will do it for you. Microsoft isn't the one who pays you anymore (I worked 2 years at Microsoft France) so start working for me instead of them" or he would ask me to migrate from Xamarin Forms to Flutter instead of MAUI haha. Anyway, I'll do what I can in the future to provide a github repro project in the future ;) I wish you a good evening! |
LOL I didn't notice it was you @jfversluis : the famous Gerald from youtube!! 😆 I post some messages on your Youtube channel few days ago (Nicolas Krier) to say how much I love your content ;) Glad to meet you here :D |
@jfversluis, just curious, can we use the sample in #7423 to repro this issue? |
I was able to repro #7423 (closed as dup of this bug). I used latest VS with .NET MAUI 6 GA. |
Adding this to .csproj file fixes the issue for me. Looks like <PropertyGroup Condition="$(TargetFramework.Contains('-windows'))">
<DefineConstants>WINDOWS</DefineConstants>
</PropertyGroup> |
@salarcode yeah that's quite odd. The MAUI targets are supposed to add that automatically... |
@salarcode I tried your solution, but it doesn't work for me. None of the Windows platform specific methods or #if WINDOWS work for me. Did you put that in your MAUI class library project file? Can you show us your .csproj xml file? |
Okay I was able to figure out why it wasn't working for me. My solution looks like this Console App with Target framework of NET 6.0, but Target OS is set to (None) under project settings. If I change my Target OS to 'Windows' for the .NET 6.0 Console App then the platform specific code in the NET 6.0 class library for Windows works. So the solution (for now) would be to have multiple .NET 6.0 Console App projects with each project targeting a specific platform (macOS, Windows) by setting the target OS. (The aforementioned .NET MAUI 6.0 class library is also used for a .NET MAUI App for cross-platform too) |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
this whole #if concept is crap, I liked the old XF way much more to write platform specific code. |
In .NET MAUI you can also place C# code files in "platform" folders and avoid using preprocessor directives such as However, I find in my own code that if I only need to special case one or two lines of code, |
yes, I know this and already use it
I use DeviceInfo.Current.Platform for this. |
Fixes #6815 Co-authored-by: Matthew Leibowitz <[email protected]>
Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you! |
Description
I want to create a service and use dependency injection.
It works on each platform except windows.
First,
#elif __WINDOWS__
wasn't taken into account by Visual Studio (see screenshot below)Next,
#elif WINDOWS
was recognized by Visual Studio but not by the compiler as it tells me it can't find the implementation of my service.It should work and the documentation MUST (just my opinion, you're the boss haha) be updated to tell to developpers what's the difference between
#if __ANDROID__
and#if ANDROID
On the Microsoft documentation I saw both, it's quite confusing. Even more when
#if __WINDOWS__
doesn't work while#if WINDOWS
does.Edit: answer given here don't use the double underscore.
Steps to Reproduce
Version with bug
Release Candidate 2 (current)
Last version that worked well
Unknown/Other
Affected platforms
Windows (I'm using Visual Studio 17.2.0 Preview 6.0)
Affected platform versions
net6.0-windows10.0.19041
Did you find any workaround?
Yes !
I handle each platform and handle WINDOWS in the
#else
But it's a dirty hack because if the problem occurs on Tizen too, it cannot work
Relevant log output
No response
The text was updated successfully, but these errors were encountered: