我创建了一个 MAUI 应用程序,它使用参考 DLL,该程序集用于对我们的服务执行 GET 和 POST 操作。
API 调用在 Android 的 Debug 模式下工作正常,但在 Release 模式下工作不正常。此问题仅发生在 Android 上,不会发生在 iOS 上。在具有发布模式的 iOS 中,应用程序可以正常工作,没有任何问题,使用程序集 API 可以正常工作。
如果在 CreateMauiApp() 下的 MauiProgram.cs 中加载 DLL 时出现任何问题,我已尝试添加 AssemblyResolve 事件代码来加载 DLL
`AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
try
{
string codeBase = Assembly.GetExecutingAssembly().Location;
if (!string.IsNullOrWhiteSpace(codeBase))
{
string filename = $"{Path.GetDirectoryName(codeBase)}/TRIMServiceModel.dll";
if (File.Exists(filename))
{
return Assembly.Load(File.ReadAllBytes(filename));
}
}
}
catch (Exception ex)
{
Debug.Write(ex.Message);
}
return null;
}`
我不确定为什么 API 仅在 Android 设备上以发布模式运行应用程序时不执行特定的 POST/GET 操作,但同样适用于 iOS(调试和发布模式)。
下面是我的 csproj 文件属性组详细信息,也可供参考:
`<PropertyGroup>
<TargetFrameworks>;net7.0-android;net7.0-ios</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);net6.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
<RootNamespace>XXXX</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<!-- Display name -->
<ApplicationTitle>XXXX</ApplicationTitle>
<!-- App Identifier -->
<ApplicationId>XXXXXXX</ApplicationId>
<ApplicationIdGuid>XXXXX-XXXX-XXXX-XXXX-XXXXXXXX</ApplicationIdGuid>
<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">16.1</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">31.0</SupportedOSPlatformVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-android|AnyCPU'">
<JavaMaximumHeapSize>5G</JavaMaximumHeapSize>
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-android|AnyCPU'">
<UseInterpreter>true</UseInterpreter>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
<CreatePackage>false</CreatePackage>
<CodesignKey>iPhone Developer</CodesignKey>
<CodesignEntitlements>Platforms\iOS\Entitlements.plist</CodesignEntitlements>
<MtouchLink>SdkOnly</MtouchLink>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-ios|AnyCPU'">
<CreatePackage>false</CreatePackage>
<UseInterpreter>true</UseInterpreter>
<PublishTrimmed>True</PublishTrimmed>
</PropertyGroup>`
任何输入都会有帮助。
问题是
Assembly.GetExecutingAssembly()
可能会产生意外的值,因为执行程序集可以在运行时更改其值,包括 Android 库的程序集。
解决方案是确保您始终获得正确的组件(即不会改变的值),例如
typeof(MainPage).Assembly
将返回一个程序集,它是您的 maui 应用程序的一部分。