在某些情况下,当从普通 .NET 类库引用对象时,MAUI 项目无法编译

问题描述 投票:0回答:1

我刚刚开始使用 MAUI。我想要 3 个项目:

  • MAUI 应用程序 (.NET Core 8)
  • 共享类库(.NET Core 8 类库)
  • Web Api (.NET Core 8)

共享类库包含一个“SearchOptions”类(TestApp.Shared.Dtos.SearchOptions),它是一个具有基本类型(int、string、bool 等)属性的简单公共类。

Web API 项目有一个带有“Search”方法的控制器,该方法接受“SearchOptions”类型的参数。

现在,问题就在这里:当我尝试在 MAUI 项目中使用“SearchOptions”类,在 http 请求中发送它时,根据我使用它的位置,我收到一条错误,指出找不到引用。

例如,如果我在 /Platforms/Android/MainActivity.cs 中使用它,则该项目构建得很好。

如果我创建一个“Helpers”文件夹和一个 SearchHelper 类 (/Helpers/SearchHelpser.cs),并在该类中使用“SearchOptions”,我会收到“错误 CS0234:类型或命名空间名称“共享”不存在于命名空间“TestApp”(您是否缺少程序集引用?)”

我的问题是:为什么它在一个地方构建得很好,而在另一个地方却不行?我缺少什么?有没有更好的方法来实现我想要做的事情? 正如我回复了一些评论一样,我的 MAUI 应用程序中已经有了项目参考。如果没有它,它就无法在“Platforms/Android/MainActivity.cs”中工作,但它确实可以......所以我认为参考是正确的。

我也知道“使用”是如何运作的。即使使用包含完整命名空间的完全限定类名,它也找不到它。

这是每个类的代码:

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(无法编译)

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(编译)

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"
            };
        }
    }
}

这是完整的错误:

严重性代码描述项目文件行抑制状态 错误CS0234命名空间“TestApp”中不存在类型或命名空间名称“Shared”(您是否缺少程序集引用?)TestApp.Maui(net8.0-android)D:\ Projects \ TestApp \ Code \ TestApp.Maui \Helpers\SearchHelper.cs 1 不适用

这是输出:

构建于上午 11:51 开始...

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 ==========

这是项目文件(以显示参考文献):

<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>
c# maui webapi class-library
1个回答
1
投票

似乎您缺少 MauiAPP -> 依赖项 -> 添加 TestApp.Shared 项目中的引用

编辑: 正如评论中所指出的,问题实际上与令人困惑的 VS 组合框有关,其中“将您的代码显示为 Android”。您可以在here查看有关该问题的 github 问题。

© www.soinside.com 2019 - 2024. All rights reserved.