.NET 7.0 MAUI安卓端无法加载XAML页面问题求助
.NET 7 MAUI 升级后安卓端无法加载XAML页面问题排查与解决
问题概述
原基于.NET 6开发的MAUI应用,在安卓设备、安卓模拟器及Windows平台均可正常运行。升级至.NET 7后,Windows端功能正常,但安卓模拟器和设备上点击登录/注册按钮时,无法加载目标XAML页面,伴随程序集加载警告及主线程卡顿日志。
相关代码
AppShell.xaml
<ShellContent ContentTemplate="{DataTemplate local:LoginPage}" FlyoutItemIsVisible="False" Shell.NavBarIsVisible="true" Shell.FlyoutBehavior="Disabled" Route="LoginPage" > </ShellContent> <ShellContent ContentTemplate="{DataTemplate local:RegisterPage}" FlyoutItemIsVisible="False" Shell.NavBarIsVisible="true" Shell.FlyoutBehavior="Disabled" Route="RegisterPage" /> <FlyoutItem Title="Home" FlyoutDisplayOptions="AsMultipleItems"> <ShellContent Title="StartGame" ContentTemplate="{DataTemplate local:StartGame}" Route="StartGame" Icon="play.png"/> <ShellContent Title="HighScore" ContentTemplate="{DataTemplate local:HighScore}" Route="HighScore" Icon="high_score.png"/> <ShellContent Title="Settings" ContentTemplate="{DataTemplate local:Settings}" Route="Settings" Icon="setting_lines.png"/> </FlyoutItem> <Shell.FlyoutFooter> <StackLayout Padding="20"> <Button Text="Sign Out" Command="{Binding SignOutCommand}" BackgroundColor="#48bfe3"/> </StackLayout> </Shell.FlyoutFooter>
AppShell.xaml.cs
public partial class AppShell : Shell { public AppShell(AppShellViewModel appShellViewModel) { InitializeComponent(); Routing.RegisterRoute($"{nameof(LoginPage)}/{nameof(RegisterPage)}", typeof(RegisterPage)); this.BindingContext = appShellViewModel; } }
登录页按钮命令
登录按钮
[RelayCommand] public async Task IsValid() { await Shell.Current.GoToAsync($"//{nameof(StartGame)}"); }
注册按钮
[RelayCommand] public async Task Tap() { await Shell.Current.GoToAsync($"//{nameof(RegisterPage)}"); }
错误日志
点击登录按钮后
[monodroid-assembly] open_from_bundles: failed to load assembly Xamarin.AndroidX.AppCompat.AppCompatResources.dll Loaded assembly: /data/data/com.companyname.sos/files/.__override__/Xamarin.AndroidX.AppCompat.AppCompatResources.dll [External] [companyname.sos] Explicit concurrent copying GC freed 3842(209KB) AllocSpace objects, 0(0B) LOS objects, 49% free, 4307KB/8615KB, paused 208us,172us total 8.310ms
点击注册按钮后
[Choreographer] Skipped 35 frames! The application may be doing too much work on its main thread. [monodroid-assembly] open_from_bundles: failed to load assembly System.Runtime.Intrinsics.dll Loaded assembly: /data/data/com.companyname.sos/files/.__override__/System.Runtime.Intrinsics.dll [External] [OnBackInvokedCallback] OnBackInvokedCallback is not enabled for the application. [OnBackInvokedCallback] Set 'android:enableOnBackInvokedCallback="true"' in the application manifest. [TabLayout] MODE_SCROLLABLE + GRAVITY_FILL is not supported, GRAVITY_START will be used instead [monodroid-assembly] open_from_bundles: failed to load assembly System.Diagnostics.TraceSource.dll Loaded assembly: /data/data/com.companyname.sos/files/.__override__/System.Diagnostics.TraceSource.dll [External] [companyname.sos] Explicit concurrent copying GC freed 8073(755KB) AllocSpace objects, 3(60KB) LOS objects, 49% free, 4178KB/8356KB, paused 206us,31us total 5.703ms
解决方案
1. 同步AndroidX依赖版本
打开项目的.csproj文件,确保所有AndroidX相关NuGet包版本与.NET 7兼容,建议统一使用最新稳定版。之后删除obj、bin目录,清理NuGet缓存并重新生成项目。
2. 调整Shell路由与跳转逻辑
- 移除
AppShell.xaml.cs中注册的嵌套路由$"{nameof(LoginPage)}/{nameof(RegisterPage)}",因为RegisterPage已作为根ShellContent存在,无需嵌套路由。 - 跳转时使用相对路由或直接路由名称,比如将注册按钮的跳转代码改为:
await Shell.Current.GoToAsync(nameof(RegisterPage));
3. 优化主线程操作
检查Tap命令中是否包含同步耗时逻辑,将其移至后台线程执行,避免主线程卡顿:
[RelayCommand] public async Task Tap() { await Task.Run(() => { // 耗时操作放在这里 }); await Shell.Current.GoToAsync(nameof(RegisterPage)); }
4. 配置AndroidManifest.xml
在AndroidManifest.xml的application标签中添加以下属性,适配Android 13+的返回键回调:
<application android:enableOnBackInvokedCallback="true"> <!-- 其他配置 --> </application>
5. 修复程序集加载警告
在项目.csproj中添加以下配置,强制嵌入相关程序集,避免加载警告:
<ItemGroup> <AndroidAsset Include="**\*.dll" /> </ItemGroup>
同时确保MAUI相关NuGet包已更新至.NET 7的最新稳定版,修复已知的程序集加载问题。
6. 彻底清理重建
删除项目的obj、bin、.vs目录,执行dotnet clean命令清理解决方案,再重新生成安卓项目,确保所有依赖正确加载。
内容的提问来源于stack exchange,提问作者DimitrisRammos
相关产品推荐
相关产品推荐

