You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI Tabbed Page工具栏不显示问题求助(Xamarin迁移)

.NET MAUI TabbedPage 工具栏不显示问题解决建议

核心原因

在.NET MAUI中,TabbedPage本身的ToolbarItems不会默认显示,因为内部嵌套的NavigationPage会接管导航栏渲染,导致TabbedPage的工具栏被覆盖。

具体解决方法

  • 将ToolbarItems移至子页面的ContentPage中
    把工具栏按钮放到每个NavigationPage包裹的ContentPage里,确保导航栏能正确渲染:

    <TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                x:Class="xxx_mobile.Views.MainPageCus"
                Title="MainPageCus"
                BarBackgroundColor="BurlyWood">
    
        <TabbedPage.Children>
            <NavigationPage Title="Sale Alerts">
                <x:Arguments>
                    <ContentPage Title="Sale Alerts">
                        <ContentPage.ToolbarItems>
                            <ToolbarItem Text="Refresh" IconImageSource="refresh.png" Priority="0" Order="Primary" Clicked="RefreshToolbarItem_Clicked" />
                            <ToolbarItem Text="Search" IconImageSource="search.png" Priority="1" Order="Primary" Clicked="SearchToolbarItem_Clicked" />
                        </ContentPage.ToolbarItems>
                        <!-- Sale Alerts页面内容 -->
                    </ContentPage>
                </x:Arguments>
            </NavigationPage>
            <NavigationPage Title="Settings">
                <x:Arguments>
                    <ContentPage Title="Settings">
                        <ContentPage.ToolbarItems>
                            <ToolbarItem Text="Refresh" IconImageSource="refresh.png" Priority="0" Order="Primary" Clicked="RefreshToolbarItem_Clicked" />
                            <ToolbarItem Text="Search" IconImageSource="search.png" Priority="1" Order="Primary" Clicked="SearchToolbarItem_Clicked" />
                        </ContentPage.ToolbarItems>
                        <!-- Settings页面内容 -->
                    </ContentPage>
                </x:Arguments>
            </NavigationPage>
        </TabbedPage.Children>
    
    </TabbedPage>
    

    如果需要在所有标签页显示相同工具栏,可将工具栏逻辑封装到基类ContentPage中,避免重复代码。

  • 改用Shell导航结构(推荐)
    MAUI的Shell是官方推荐的导航方案,天然支持全局工具栏+标签页,配置更简洁:

    <Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
           xmlns:views="clr-namespace:xxx_mobile.Views"
           x:Class="xxx_mobile.AppShell"
           Title="MainPageCus">
    
        <Shell.ToolbarItems>
            <ToolbarItem Text="Refresh" IconImageSource="refresh.png" Priority="0" Order="Primary" Clicked="RefreshToolbarItem_Clicked" />
            <ToolbarItem Text="Search" IconImageSource="search.png" Priority="1" Order="Primary" Clicked="SearchToolbarItem_Clicked" />
        </Shell.ToolbarItems>
    
        <TabBar>
            <Tab Title="Sale Alerts">
                <ShellContent ContentTemplate="{DataTemplate views:SaleAlertsPage}" />
            </Tab>
            <Tab Title="Settings">
                <ShellContent ContentTemplate="{DataTemplate views:SettingsPage}" />
            </Tab>
        </TabBar>
    </Shell>
    

    这种方式下,工具栏会全局显示在所有标签页顶部,无需在每个页面重复配置。

  • 检查资源与平台配置

    • 确认refresh.png、search.png已正确添加到项目对应平台的资源目录(如Android的Resources/Drawable、iOS的Resources/Images),且生成操作设置正确(Android设为AndroidResource,iOS设为BundleResource)。
    • 移除无效的android:TabbedPage.ToolbarPlacement="Top"属性,该设置在嵌套NavigationPage的场景下不生效。

内容的提问来源于stack exchange,提问作者Joey007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 14:47:03