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

Xamarin.Forms隐藏导航栏后,如何在蓝色返回栏显示页面标题?

实现方案

你提到的带返回按钮的蓝色栏是平台原生导航栏(iOS的UINavigationBar或Android的ActionBar),以下提供两种适配方案:

方案一:自定义Forms导航栏(跨平台推荐)

这种方式不需要隐藏Forms导航栏,直接通过样式自定义导航栏外观,同时保留标题和返回按钮:

  1. 移除页面CS代码中的NavigationPage.SetHasNavigationBar(this, false);
  2. 在App.xaml中添加全局导航栏样式:
<Application.Resources>
    <Style TargetType="NavigationPage">
        <!-- 设置导航栏背景色为你需要的蓝色 -->
        <Setter Property="BarBackgroundColor" Value="Blue" />
        <!-- 设置标题文字颜色 -->
        <Setter Property="BarTextColor" Value="White" />
    </Style>
</Application.Resources>
  1. 页面XAML保留Title="{Binding Title}",导航栏会自动显示绑定的标题和返回按钮。

方案二:自定义渲染器控制原生导航栏(适合必须隐藏Forms导航栏的场景)

如果必须隐藏Forms导航栏,需要针对iOS和Android分别编写自定义渲染器,直接控制原生导航栏显示标题:

iOS端渲染器

在iOS项目中创建如下渲染器类:

using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using İdaServis.View; // 替换为你的页面所在命名空间

[assembly: ExportRenderer(typeof(PageName), typeof(PageNameRenderer))]
namespace İdaServis.iOS.Renderers
{
    public class PageNameRenderer : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
            
            if (NavigationController != null)
            {
                // 设置原生导航栏标题
                NavigationController.TopViewController.Title = ((PageName)Element).Title;
                // 确保返回按钮显示
                NavigationController.TopViewController.NavigationItem.HidesBackButton = false;
                // 设置导航栏背景色
                NavigationController.NavigationBar.BarTintColor = UIColor.Blue;
                // 设置标题文字颜色
                NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes
                {
                    ForegroundColor = UIColor.White
                };
            }
        }
    }
}

Android端渲染器

在Android项目中创建如下渲染器类:

using Android.App;
using Android.Content;
using Android.Graphics;
using AndroidX.AppCompat.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using İdaServis.View; // 替换为你的页面所在命名空间

[assembly: ExportRenderer(typeof(PageName), typeof(PageNameRenderer))]
namespace İdaServis.Droid.Renderers
{
    public class PageNameRenderer : PageRenderer
    {
        public PageNameRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);
            
            if (e.NewElement != null)
            {
                var page = (PageName)e.NewElement;
                var actionBar = ((Activity)Context).SupportActionBar;
                if (actionBar != null)
                {
                    // 显示返回按钮
                    actionBar.SetDisplayHomeAsUpEnabled(true);
                    // 设置原生导航栏标题
                    actionBar.Title = page.Title;
                    // 设置导航栏背景色
                    actionBar.SetBackgroundDrawable(new ColorDrawable(Color.Blue.ToAndroid()));
                    // 设置标题文字颜色
                    actionBar.SetTitleTextColor(Color.White.ToAndroid());
                }
            }
        }
    }
}

页面CS中保留NavigationPage.SetHasNavigationBar(this, false);即可实现需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 09:35:34