首先,我使用 .NET 6.0 构建了我的应用程序,并且我的应用程序在 Android 设备、Android 模拟器和 Windows 机器上取得了成功。
之后,我将项目从 .NET 6.0 升级到 .NET 7.0。 我的代码适用于 Windows 机器,但不适用于 Android 模拟器和 Android 设备。
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;
}
}
我有.xaml(和.xaml.cs文件)登录页面:
当我触摸按钮时,C# 代码是:
[RelayCommand]
public async Task IsValid()
{
await Shell.Current.GoToAsync($"//{nameof(StartGame)}");
}
错误是:
[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
StartGame() 构造函数之后调用的错误
[RelayCommand]
public async Task Tap()
{
await Shell.Current.GoToAsync($"//{nameof(RegisterPage)}");
}
在函数 Tap 中创建错误:
[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
.Net 6.0 的相同代码成功。
我不认为这是一个错误,这是 Android SDK 版本之间行为的改变。
当您从 .NET 6 更新到 .NET 7 时,您还会隐式更新目标 Android SDK 版本。
如果您检查包含的最后一点日志记录,则已经提供了可能的解决方案:
Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
有关更多信息,请访问:https://developer.android.com/guide/navigation/custom-back/predictive-back-gesture