如何在 .Net Maui 中创建选项卡式页面?

Tho*_*ton 5 c# android maui

我在 .Net Maui 有一个选项卡式页面,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     x:Class="MyApp.Pages.SomeClass"
    xmlns:Controls="clr-namespace:MyApp.Resources.Controls"
    xmlns:local="clr-namespace:MyApp.Pages"
                     Title="Some title">
    <local:Page1 />
    <local:Page2 />
    <local:Page3 />
</TabbedPage>
Run Code Online (Sandbox Code Playgroud)

这些页面或多或少相同:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     x:Class="MyApp.Pages.Page1"
    xmlns:Controls="clr-namespace:MyApp.Resources.Controls"
    xmlns:VerticalStackLayout="clr-namespace:MyApp.Resources.Controls"      
                     Title="Vente">
    <VerticalStackLayout>
        <Label Text="Page 1"/>
    </VerticalStackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

C# 代码:

public partial class Page1: ContentPage {
  public Page1() {
    InitializeComponent();
  }
}
Run Code Online (Sandbox Code Playgroud)

选项卡式页面在 Windows 上正常工作:

截屏

然而,在物理 Android 设备上,打开选项卡式页面时出现以下异常。这些页面都没有 C# 内容。它们100%是空的。

0xFFFFFFFFFFFFFFFF in Android.Runtime.JNIEnv.monodroid_debugger_unhandled_exception C#
0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5 C#
0x23 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:353,26    C#
0xEE in Microsoft.Maui.Controls.Handlers.TabbedPageManager.SetTabLayout at D:\a\_work\1\s\src\Controls\src\Core\Platform\Android\TabbedPageManager.cs:240,5 C#
0x1BD in Microsoft.Maui.Controls.Handlers.TabbedPageManager.SetElement at D:\a\_work\1\s\src\Controls\src\Core\Platform\Android\TabbedPageManager.cs:162,5  C#
0x20 in Microsoft.Maui.Controls.TabbedPage.CreatePlatformView at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\TabbedPage\TabbedPage.Android.cs:23,4 C#
0x10 in Microsoft.Maui.Controls.TabbedPage.OnCreatePlatformView at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\TabbedPage\TabbedPage.Android.cs:30,5   C#
0x15 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.ITabbedView,Android.Views.View>.OnCreatePlatformView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:72,4 C#
0x1 in Microsoft.Maui.Handlers.ViewHandler.OnCreatePlatformElement at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:119,4    C#
0x1 in Microsoft.Maui.Handlers.ElementHandler.CreatePlatformElement at D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:96,4  C#
0x49 in Microsoft.Maui.Handlers.ElementHandler.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\Element\ElementHandler.cs:52,4    C#
0x2 in Microsoft.Maui.Handlers.ViewHandler.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandler.cs:125,4 C#
0x2 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.ITabbedView,Android.Views.View>.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:53,4    C#
0x7 in Microsoft.Maui.Handlers.ViewHandler<Microsoft.Maui.ITabbedView,Android.Views.View>.SetVirtualView at D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:56,4    C#
0x65 in Microsoft.Maui.Controls.Element.SetHandler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:64,6 C#
0x2 in Microsoft.Maui.Controls.Element.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\Element\Element.Impl.cs:20,11    C#
0x18 in Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler at D:\a\_work\1\s\src\Controls\src\Core\HandlerImpl\VisualElement\VisualElement.Impl.cs:301,5 C#
0xB0 in Microsoft.Maui.Platform.ElementExtensions.ToHandler at D:\a\_work\1\s\src\Core\src\Platform\ElementExtensions.cs:96,4   C#
0x2 in Microsoft.Maui.Platform.ViewExtensions.ToHandler at D:\a\_work\1\s\src\Core\src\Platform\ViewExtensions.cs:44,4  C#
0xAA in Microsoft.Maui.Controls.Platform.Compatibility.ShellContentFragment.OnCreateView at D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellContentFragment.cs:145,4 C#
0x24 in AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ at C:\a\_work\3\s\generated\androidx.fragment.fragment\obj\Release\net6.0-android\generated\src\AndroidX.Fragment.App.Fragment.cs:1999,4 C#
0xD in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:352,5  C#
Run Code Online (Sandbox Code Playgroud)

.Net Maui 选项卡式页面可以在 Android 上运行吗?

And*_*obs 3

如果您有兴趣使用 TabbedPage 浏览页面集合,请参阅此处的 maui 示例:TabbedPage

它可以在我的 Android 物理设备上运行。

另一种选择是使用 shell 选项卡栏,如以下示例所示:ShellTabBar