Search code examples
c#mauiwebapiclass-library

MAUI project does not compile, in certains cases, when referencing objects from a normal .NET Class Library


I just started using MAUI. I want to have 3 projects:

  • The MAUI app (.NET Core 8)
  • A Shared class library (.NET Core 8 Class Library)
  • A Web Api (.NET Core 8)

The Shared class library contains a class "SearchOptions" (TestApp.Shared.Dtos.SearchOptions) which is a simple public class with basic type (int, string, bool, etc.) properties.

The Web API project has a controller with a "Search" method which accepts a parameter of type "SearchOptions".

Now, here lies the problem: When I try to use the "SearchOptions" class in the MAUI project, to send it in a http request, depending on where I use it, I get an error saying the reference cannot be found.

If I use it in /Platforms/Android/MainActivity.cs, for example, the project builds fine.

If I create a "Helpers" folder and a SearchHelper class (/Helpers/SearchHelpser.cs), and use "SearchOptions" in that class, I get an "error CS0234 : The type or namespace name 'Shared' does not exist in the namespace 'TestApp' (are you missing an assembly reference?)"

My question is: How come it builds fine in one place and not in another place? What am I missing? Is there a better way to achieve what I am trying to do? As I have replied to some of the comments, I already have the project reference in my MAUI app. Without it, it would not work in "Platforms/Android/MainActivity.cs", but it does... so I assume the reference is correct.

I also know how "using" works. Even with a fully qualified class name that includes the full namespace, it does not find it.

Here is the code for each class:

TestApp.Shared.Dtos/Search/SearchOptions.cs:

namespace TestApp.Shared.Dtos.Search
{
    public class SearchOptions
    {
        public string Terms { get; set; } = string.Empty;
        public SearchTypes SearchType { get; set; }
    }
}

TestApp.Maui/Helpers/SearchHelper.cs (Does not compile)

using TestApp.Shared.Dtos.Search;

namespace TestApp.Maui.Helpers
{
    internal class SearchHelper
    {
        public void Search()
        {
            var options = new SearchOptions
            {
                SearchType = SearchTypes.ByCar,
                Terms = "Test Terms For Search"
            };
        }
    }
}

TestApp.Maui/Platforms/Android/MainActivity.cs (Compiles)

using Android.App;
using Android.Content.PM;
using TestApp.Shared.Dtos.Search;

namespace TestApp.Maui
{
    [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
    public class MainActivity : MauiAppCompatActivity
    {
        public void TestMethod()
        {
            var options = new SearchOptions
            {
                SearchType = SearchTypes.ByCar,
                Terms = "Test Terms For Search"
            };
        }
    }
}

Here is the complete error:

Severity Code Description Project File Line Suppression State Error CS0234 The type or namespace name 'Shared' does not exist in the namespace 'TestApp' (are you missing an assembly reference?) TestApp.Maui (net8.0-android) D:\Projects\TestApp\Code\TestApp.Maui\Helpers\SearchHelper.cs 1 N/A

Here is the output:

Build started at 11:51 AM...

1>------ Build started: Project: TestApp.Maui, Configuration: Debug Any CPU ------
1>D:\Projects\TestApp\Code\TestApp.Maui\Helpers\SearchHelper.cs(1,17,1,23): error CS0234: The type or namespace name 'Shared' does not exist in the namespace 'TestApp' (are you missing an assembly reference?)
1>Done building project "TestApp.Maui.csproj" -- FAILED.
1>TestApp.Maui -> D:\Projects\TestApp\Code\TestApp.Maui\bin\Debug\net8.0-android\TestApp.Maui.dll
1>TestApp.Maui -> D:\Projects\TestApp\Code\TestApp.Maui\bin\Debug\net8.0-maccatalyst\maccatalyst-x64\TestApp.Maui.dll
1>TestApp.Maui -> D:\Projects\TestApp\Code\TestApp.Maui\bin\Debug\net8.0-windows10.0.19041.0\win10-x64\TestApp.Maui.dll
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
========== Build completed at 11:51 AM and took 02.217 seconds ==========

Here is the project file (to show the reference is there):

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
        <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
        <!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->

        <!-- Note for MacCatalyst:
        The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
        When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>.
        The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated;
        either BOTH runtimes must be indicated or ONLY macatalyst-x64. -->
        <!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->

        <OutputType>Exe</OutputType>
        <RootNamespace>TestApp.Maui</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>

        <!-- Display name -->
        <ApplicationTitle>TestApp.Maui</ApplicationTitle>

        <!-- App Identifier -->
        <ApplicationId>com.companyname.TestApp.maui</ApplicationId>

        <!-- Versions -->
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
    </PropertyGroup>

    <ItemGroup>
        <!-- App Icon -->
        <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

        <!-- Splash Screen -->
        <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />

        <!-- Custom Fonts -->
        <MauiFont Include="Resources\Fonts\*" />

        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\TestApp.Shared.Dtos\TestApp.Shared.Dtos.csproj" />
    </ItemGroup>

</Project>

Solution

  • Seems like you are missing the reference in the MauiAPP -> Dependencies -> Add TestApp.Shared project

    EDIT: As pointed out in comments, the problem is actually related to a confusing VS combobox where it 'Shows your code as Android'. You can check the github issue about that here.