Skip to content
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

Closed
nk54 opened this issue May 4, 2022 · 29 comments · Fixed by #12680
Closed

#elif WINDOWS doesn't work #6815

nk54 opened this issue May 4, 2022 · 29 comments · Fixed by #12680
Assignees
Labels
area-single-project Splash Screen, Multi-Targeting, MauiFont, MauiImage, MauiAsset, Resizetizer fixed-in-7.0.59 Look for this fix in 7.0.59! fixed-in-7.0.100 fixed-in-7.0.101 fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! platform/windows 🪟 s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@nk54
Copy link

nk54 commented May 4, 2022

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)
image

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

  • create an interface inside the MAUI project
  • create an implementation of that interface inside each platforms folder
  • try to register the service inside MauiProgram.cs

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

@nk54 nk54 added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels May 4, 2022
@Eilon
Copy link
Member

Eilon commented May 4, 2022

In my experience, the underscore versions of the define's are never used. Only the "text" defines, such as #if WINDOWS, etc. There are also version-specific defines, such as WINDOWS10_0_19041_0, but those are rarely used, because most code differs only by platform, and not specific version of the platform.

Can you show a code sample that doesn't work if you use only the "text" defines?

@Eilon
Copy link
Member

Eilon commented May 4, 2022

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"):

image

@jfversluis
Copy link
Member

We're using this in various places in our own code and is described in the docs here as being #if WINDOWS.

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.

@nk54
Copy link
Author

nk54 commented May 5, 2022

Hi @Eilon thank you for your time.

In this thread on the maui documentation, it seems we have to use #if PLATFORMNAME instead of #if __PLATFORMNAME__

If I set #if WINDOWS the compiler doesn't find my class whereas the overview of the type in Visual Studio indicates it's found on each platform

@nk54
Copy link
Author

nk54 commented May 5, 2022

@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

@jfversluis
Copy link
Member

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?

@nk54
Copy link
Author

nk54 commented May 5, 2022

image

@nk54
Copy link
Author

nk54 commented May 5, 2022

@jfversluis

I've written the steps : go to my message.
image

image

Add in MauiProgram this

#if ANDROID
using SimpleApp.Platforms.Android;
#elif IOS
using SimpleApp.Platforms.iOS;
#elif WINDOWS
using SimpleApp.Platforms.Windows;
#endif

Register the service
image

THE BUG

With #elif WINDOWS the compiler doesn't find TestService.
Replace #elif WINDOWS by #else the compiler finds the class.

It is cleaarly a bug.

The code of service if you want to repro.
// Path SimpleApp/ITestService.cs public interface ITestService { void PrintPlatform(); } // Path SimpleApp/Platforms/Windows/TestService.cs namespace SimpleApp.Windows { public class TestService : ITestService { public void PrintPlatform() => Debug.WriteLine("Windows"); } } // Path SimpleApp/Platforms/Android/TestService.cs namespace SimpleApp.Android { public class TestService : ITestService { public void PrintPlatform() => Debug.WriteLine("Android"); } } // Path SimpleApp/Platforms/iOS/TestService.cs namespace SimpleApp.iOS { public class TestService : ITestService { public void PrintPlatform() => Debug.WriteLine("iOS"); } }

@jfversluis
Copy link
Member

This seems to work fine for me: https://github.com/jfversluis/Repro6815

Just have to make sure that either all platforms have the TestService or exclude the registration for the platforms that are not supported or remove the TargetFrameworks from the csproj that you're not going to use

@nk54
Copy link
Author

nk54 commented May 5, 2022

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

@jfversluis jfversluis reopened this May 5, 2022
@jfversluis jfversluis added area-single-project Splash Screen, Multi-Targeting, MauiFont, MauiImage, MauiAsset, Resizetizer and removed s/needs-verification Indicates that this issue needs initial verification before further triage will happen labels May 5, 2022
@jfversluis jfversluis added this to the 6.0.300-servicing milestone May 5, 2022
@jfversluis
Copy link
Member

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.

@nk54
Copy link
Author

nk54 commented May 5, 2022

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!

@nk54
Copy link
Author

nk54 commented May 5, 2022

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

@VincentBu VincentBu added the s/verified Verified / Reproducible Issue ready for Engineering Triage label May 24, 2022
@VincentBu
Copy link

@jfversluis, just curious, can we use the sample in #7423 to repro this issue?

@Eilon
Copy link
Member

Eilon commented May 24, 2022

I was able to repro #7423 (closed as dup of this bug). I used latest VS with .NET MAUI 6 GA.

@salarcode
Copy link
Contributor

salarcode commented Jul 31, 2022

Adding this to .csproj file fixes the issue for me. Looks like WINDOWS is not defined

<PropertyGroup Condition="$(TargetFramework.Contains('-windows'))">
    <DefineConstants>WINDOWS</DefineConstants>
</PropertyGroup>

@Eilon
Copy link
Member

Eilon commented Aug 1, 2022

@salarcode yeah that's quite odd. The MAUI targets are supposed to add that automatically...

@iron4548
Copy link

iron4548 commented Aug 9, 2022

@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?

@iron4548
Copy link

iron4548 commented Aug 9, 2022

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.
.NET MAUI 6.0 class library with platform specific code, and this library is linked to the NET 6.0 Console app.

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)

@mattleibow mattleibow modified the milestones: .NET 7 Planning, Backlog Nov 3, 2022
@ghost
Copy link

ghost commented Nov 3, 2022

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.

@MrDeej
Copy link

MrDeej commented Dec 5, 2022

Hmm...
image
image
image

Does not work when using classes from other project of type .NET7.0

@MagicAndre1981
Copy link
Contributor

this whole #if concept is crap, I liked the old XF way much more to write platform specific code.

@Eilon
Copy link
Member

Eilon commented Dec 5, 2022

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 #if. Learn more here: https://learn.microsoft.com/dotnet/maui/platform-integration/invoke-platform-code?view=net-maui-7.0#partial-classes-and-methods

However, I find in my own code that if I only need to special case one or two lines of code, #if is easier. I use platform folders when I have entire classes that are almost entirely different.

@MagicAndre1981
Copy link
Contributor

In .NET MAUI you can also place C# code files in "platform" folders and avoid using preprocessor directives such as #if. Learn more here: https://learn.microsoft.com/dotnet/maui/platform-integration/invoke-platform-code?view=net-maui-7.0#partial-classes-and-methods

yes, I know this and already use it

However, I find in my own code that if I only need to special case one or two lines of code, #if is easier. I use platform folders when I have entire classes that are almost entirely different.

I use DeviceInfo.Current.Platform for this.

mattleibow added a commit that referenced this issue Jan 16, 2023
@mattleibow mattleibow removed this from the Backlog milestone Jan 16, 2023
@mattleibow mattleibow moved this from Todo to In Progress in MAUI SDK Ongoing Jan 16, 2023
@mattleibow mattleibow self-assigned this Jan 16, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in MAUI SDK Ongoing Jan 16, 2023
github-actions bot pushed a commit that referenced this issue Jan 16, 2023
mattleibow added a commit that referenced this issue Jan 16, 2023
@samhouts samhouts added the fixed-in-7.0.59 Look for this fix in 7.0.59! label Feb 16, 2023
@samhouts samhouts added this to the .NET 7 + Servicing milestone Feb 16, 2023
@samhouts samhouts added the fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! label Feb 22, 2023
@ghost
Copy link

ghost commented Mar 6, 2023

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!

@ghost ghost locked as resolved and limited conversation to collaborators Apr 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-single-project Splash Screen, Multi-Targeting, MauiFont, MauiImage, MauiAsset, Resizetizer fixed-in-7.0.59 Look for this fix in 7.0.59! fixed-in-7.0.100 fixed-in-7.0.101 fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! platform/windows 🪟 s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.