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

Xamarin Forms(WPF)中FormsContentPage隐藏标题栏的实现方案

解决Xamarin Forms(WPF平台)隐藏标题栏并全屏的问题

针对你基于Fresh MVVM的Xamarin Forms项目,在WPF平台使用FormsContentPage展示播放器页面时无法隐藏标题栏的问题,可按以下方案处理:

1. 修正原有代码的语法错误

你提供的代码中最后一行末尾是逗号(this.ParentWindow.Height = SystemParameters.VirtualScreenHeight,),需改为分号,否则会导致代码执行异常。

2. 正确获取WPF主窗口并设置全屏属性

建议通过Application.Current.MainWindow获取主窗口(比ParentWindow更可靠,避免嵌套容器导致的获取失败),并在页面的OnAppearing事件中执行全屏设置:

using System.Windows;
using Xamarin.Forms;
using Xamarin.Forms.Platform.WPF;

public partial class PlayerPage : ContentPage
{
    public PlayerPage()
    {
        InitializeComponent();
        // 隐藏Xamarin Forms的导航栏
        NavigationPage.SetHasNavigationBar(this, false);
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        if (Device.RuntimePlatform == Device.WPF)
        {
            var mainWindow = Application.Current.MainWindow as Window;
            if (mainWindow != null)
            {
                // 隐藏标题栏
                mainWindow.WindowStyle = WindowStyle.None;
                // 禁止调整窗口大小
                mainWindow.ResizeMode = ResizeMode.NoResize;
                // 最大化窗口
                mainWindow.WindowState = WindowState.Maximized;
                // 覆盖整个虚拟屏幕(适配多显示器)
                mainWindow.Left = 0;
                mainWindow.Top = 0;
                mainWindow.Width = SystemParameters.VirtualScreenWidth;
                mainWindow.Height = SystemParameters.VirtualScreenHeight;
            }
        }
    }

    // 可选:退出页面时恢复窗口样式
    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        if (Device.RuntimePlatform == Device.WPF)
        {
            var mainWindow = Application.Current.MainWindow as Window;
            if (mainWindow != null)
            {
                mainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                mainWindow.ResizeMode = ResizeMode.CanResize;
                mainWindow.WindowState = WindowState.Normal;
            }
        }
    }
}

3. 验证Fresh MVVM的页面导航配置

确保在Fresh MVVM导航到该页面时,没有额外包裹不必要的NavigationPage,或者已正确设置导航栏隐藏。例如在PageModel的导航代码中:

await CoreMethods.PushPageModel<PlayerPageModel>(modal: false, animate: true);

若页面作为模态页展示,同样需要确保模态页的导航栏被隐藏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 01:52:37