question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error CS0246 on iOS namespaces when using Conditional Compilation

See original GitHub issue

Description

Hello everyone,

given the following Effect sample:

using Microsoft.Maui.Controls.Platform;
#if ANDROID
using Android.Views;
#elif IOS
using UIKit;
using Foundation;
#endif

namespace TestMauiEffect.Effects;

public class TestEffect : RoutingEffect
{
    public static readonly BindableProperty TestProperty = BindableProperty.CreateAttached(
                                                                        nameof(Test),
                                                                        typeof(bool),
                                                                        typeof(TestEffect),
                                                                        false,
                                                                        propertyChanged: TestPropertyChanged);

    public static bool Test => true;

    static void TestPropertyChanged(BindableObject? bindable, object oldValue, object newValue)
    {
    }
    public TestEffect()
        : base("TestMauiEffect.TestEffect")
            {
            }
}


public class TestPlaftormEffect : PlatformEffect
{
#if ANDROID
    protected override void OnAttached()
    {
        //throw new NotImplementedException();
    }

    protected override void OnDetached()
    {
        //throw new NotImplementedException();
    }
#elif IOS
    protected override void OnAttached()
    {
        //throw new NotImplementedException();
    }

    protected override void OnDetached()
    {
        //throw new NotImplementedException();
    }
#endif
}

No problem on Android: the namespace is correctly resolved and found, and I can access its classes and such in the PlatformEffect. On iOS though I get a CS0246: The type or namespace name 'UIKit' could not be found (are you missing a using directive or an assembly reference?). It seems I cannot use iOS namespaces in any place that is not the “Platforms” folder. I also tried moving the net7.0-ios TargetFramework to be the first in the list in the app .csproj file but it did not help (found this as a possible solution somewhere). ALSO, Visual Studio (for Mac) does not seem to correctly reflect the selected platform when greying-out those sections. Android is always the colored one.

Visual Studio Info
Visual Studio Community 2022 for Mac
Version 17.6.2 (build 405)
Installation UUID: feae478c-625f-496a-ba48-6679f58faa9b

Runtime
.NET 7.0.3 (64-bit)
Architecture: X64
Microsoft.macOS.Sdk 13.1.1007; git-rev-head:8afca776a0a96613dfb7200e0917bb57f9ed5583; git-branch:release/7.0.1xx-xcode14.2

Roslyn (Language Service)
4.6.0-3.23180.6+99e956e42697a6dd886d1e12478ea2b27cceacfa

NuGet
Version: 6.4.0.117

.NET SDK (x64)
SDK: /usr/local/share/dotnet/sdk/7.0.306/Sdks
SDK Versions:
	7.0.306
	7.0.304
	7.0.302
	7.0.203
	7.0.202
	7.0.201
	7.0.200
	7.0.103
	7.0.102
	7.0.101
	7.0.100
	6.0.412
	6.0.410
	6.0.408
	6.0.407
	6.0.406
	6.0.405
	6.0.404
	6.0.403
	6.0.402
	6.0.401
	6.0.400
	6.0.310
	6.0.302
	6.0.301
	6.0.300
	6.0.105
	6.0.104
	6.0.103
	6.0.102
	6.0.101
	6.0.100-preview.3.21202.5
	6.0.100-preview.2.21155.3
	6.0.100-preview.1.21103.13
	5.0.408
	5.0.407
	5.0.406
	5.0.405
	5.0.404
	5.0.403
	5.0.402
	5.0.401
	5.0.400
	5.0.302
	5.0.301
	5.0.203
	5.0.202
	5.0.201
	5.0.103
	5.0.102
	5.0.101
	5.0.100
	3.1.426
	3.1.425
	3.1.424
	3.1.423
	3.1.422
	3.1.421
	3.1.420
	3.1.419
	3.1.418
	3.1.417
	3.1.416
	3.1.415
	3.1.414
	3.1.413
	3.1.412
	3.1.411
	3.1.410
	3.1.409
	3.1.408
	3.1.407
	3.1.406
	3.1.405
	3.1.404
	3.1.403
	3.1.402
	3.1.401
	3.1.302
	3.1.301
	3.1.300
	3.1.202
	3.1.200
	3.1.102
	3.1.101
	3.1.100
	3.0.101
	3.0.100
	2.1.701
MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks

.NET Runtime (x64)
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	7.0.9
	7.0.7
	7.0.5
	7.0.4
	7.0.3
	7.0.2
	7.0.1
	7.0.0
	6.0.20
	6.0.18
	6.0.16
	6.0.15
	6.0.14
	6.0.13
	6.0.12
	6.0.11
	6.0.10
	6.0.9
	6.0.8
	6.0.7
	6.0.6
	6.0.5
	6.0.4
	6.0.3
	6.0.2
	6.0.1
	6.0.0-preview.3.21201.4
	5.0.17
	5.0.16
	5.0.15
	5.0.14
	5.0.13
	5.0.12
	5.0.11
	5.0.10
	5.0.9
	5.0.8
	5.0.7
	5.0.6
	5.0.5
	5.0.4
	5.0.3
	5.0.2
	5.0.1
	5.0.0
	3.1.32
	3.1.31
	3.1.30
	3.1.29
	3.1.28
	3.1.27
	3.1.26
	3.1.25
	3.1.24
	3.1.23
	3.1.22
	3.1.21
	3.1.20
	3.1.19
	3.1.18
	3.1.17
	3.1.16
	3.1.15
	3.1.14
	3.1.13
	3.1.12
	3.1.11
	3.1.10
	3.1.9
	3.1.8
	3.1.7
	3.1.6
	3.1.5
	3.1.4
	3.1.2
	3.1.1
	3.1.0
	3.0.1
	3.0.0
	2.1.23
	2.1.22
	2.1.21
	2.1.20
	2.1.19
	2.1.18
	2.1.17
	2.1.16
	2.1.15
	2.1.14
	2.1.13
	2.1.12

Xamarin.Profiler
Version: 1.8.0.49
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater
Version: 11

Xamarin.Android
Version: 13.2.0.6 (Visual Studio Community)
Commit: xamarin-android/d17-5/a200af1
Android SDK: /Users/mpazzaglia/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		12.0 (API level 31)
		8.1  (API level 27)
		5.0  (API level 21)
		11.0 (API level 30)
		10.0 (API level 29)
		9.0  (API level 28)
		13.0 (API level 33)

SDK Command-line Tools Version: 7.0
SDK Platform Tools Version: 34.0.4
SDK Build Tools Version: 33.0.0

Build Information: 
Mono: 6dd9def
Java.Interop: xamarin/java.interop/d17-5@149d70fe
SQLite: xamarin/sqlite/3.40.1@68c69d8
Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-5@9f02d77

Microsoft Build of OpenJDK
Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk
11.0.16.1
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Eclipse Temurin JDK
Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk
1.8.0.302
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager
Version: 17.6.0.50
Hash: a715dca
Branch: HEAD
Build date: 2023-07-26 17:09:02 UTC

Android Device Manager
Version: 0.0.0.1309
Hash: 06e3e77
Branch: HEAD
Build date: 2023-07-26 17:09:02 UTC

Apple Developer Tools
Xcode: 14.3.1 21815
Build: 14E300c

Xamarin.Mac
Version: 9.3.0.6 Visual Studio Community
Hash: 97731c92c
Branch: xcode14.3
Build date: 2023-04-11 22:38:35-0400

Xamarin.iOS
Version: 16.4.0.6 Visual Studio Community
Hash: 97731c92c
Branch: xcode14.3
Build date: 2023-04-11 22:38:36-0400

Xamarin Designer
Version: 17.6.3.9
Hash: 2648399ae8
Branch: remotes/origin/d17-6
Build date: 2023-07-26 17:08:56 UTC

Build Information
Release ID: 1706020405
Git revision: beaff930b1a0035595265bd63d6ef6d670e93e9e
Build date: 2023-07-26 17:07:10+00
Build branch: release-17.6
Build lane: release-17.6

Operating System
Mac OS X 13.0.1
Darwin 22.1.0 Darwin Kernel Version 22.1.0
    Sun Oct  9 20:14:54 PDT 2022
    root:xnu-8792.41.9~2/RELEASE_X86_64 x86_64

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

7.0.92

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

0reactions
Eiloncommented, Aug 8, 2023

Hey there, sure here it is: https://developercommunity.visualstudio.com/t/NET-MAUI-get-CS0246-and-greying-out-is/10432416

Thanks! I’m closing this issue here because the linked feedback issue is the main item to track. If anyone else is running into this same issue, please add your feedback and votes to that issue. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why am I getting error CS0246: The type or namespace ...
Resolve this issue by updating the Target Framework in the project application settings. For instance, In my case the project was compiling with ......
Read more >
Errors importing Apple.Core package on Windows machine
Errors importing Apple. ... error CS0246: The type or namespace name 'PlistDocument' could not be found (are you missing a using directive or...
Read more >
The type or namespace name XXX could not be found
Error CS0246: The type or namespace name 'XXX' could not be found (are you missing a using directive or an assembly reference?)
Read more >
What is CS0246?
This error is caused when the namespace that you are trying to use does not exist. Error presented in this example: error CS0246:...
Read more >
Manual: Namespaces
If two namespaces that contain clashing class names are imported with using directives at the same time, the compiler will report an error....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found