如何在Xamarin Forms(WPF)中用Fresh MVVM隐藏标题栏并全屏显示
在Xamarin Forms WPF项目(Fresh MVVM)中实现全屏无标题栏播放器页面
针对你设置WindowStyle=None和AllowTransparency=false后仍残留标题栏的问题,可以通过以下步骤彻底实现全屏无框效果:
1. 修正WPF主窗口的XAML配置
打开WPF项目中的MainWindow.xaml,确保窗口属性配置完整,覆盖标题栏显示和全屏状态:
<Window x:Class="YourAppNamespace.WPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:forms="clr-namespace:Xamarin.Forms.Platform.WPF;assembly=Xamarin.Forms.Platform.WPF" Title="MainWindow" WindowStyle="None" WindowState="Maximized" AllowsTransparency="False" ResizeMode="NoResize"> <Grid> <forms:WindowsPageHost x:Name="PageHost"/> </Grid> </Window>
WindowState="Maximized"强制窗口全屏显示ResizeMode="NoResize"禁用窗口拖拽调整,避免标题栏相关交互残留
2. 通过Fresh MVVM结合平台服务动态控制全屏
因为需要在播放器页面切换全屏状态,可借助Xamarin的依赖服务实现平台特定逻辑:
步骤1:在共享项目定义窗口服务接口
public interface IWindowService { void ToggleFullScreen(bool enableFullScreen); }
步骤2:在WPF项目实现接口
[assembly: Dependency(typeof(WpfWindowService))] namespace YourAppNamespace.WPF { public class WpfWindowService : IWindowService { public void ToggleFullScreen(bool enableFullScreen) { var mainWindow = Application.Current.MainWindow as MainWindow; if (mainWindow == null) return; mainWindow.WindowStyle = enableFullScreen ? WindowStyle.None : WindowStyle.SingleBorderWindow; mainWindow.WindowState = enableFullScreen ? WindowState.Maximized : WindowState.Normal; mainWindow.ResizeMode = enableFullScreen ? ResizeMode.NoResize : ResizeMode.CanResize; } } }
步骤3:在播放器页面模型中调用服务
在PlayerPageModel(Fresh MVVM的页面模型)的初始化方法中触发全屏:
public class PlayerPageModel : FreshBasePageModel { public override void Init(object initData) { base.Init(initData); // 启用全屏无框 var windowService = DependencyService.Get<IWindowService>(); windowService.ToggleFullScreen(true); // 隐藏Xamarin Forms自带导航栏 CurrentPage.NavigationPage.HasNavigationBar = false; } // 退出页面时恢复窗口状态(可选) public override void ReverseInit(object returnedData) { base.ReverseInit(returnedData); var windowService = DependencyService.Get<IWindowService>(); windowService.ToggleFullScreen(false); CurrentPage.NavigationPage.HasNavigationBar = true; } }
3. 隐藏Xamarin Forms页面导航栏
确保播放器页面本身的导航栏也被隐藏,避免额外的顶部栏:
在PlayerPage.xaml中添加属性:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="YourAppNamespace.PlayerPage" NavigationPage.HasNavigationBar="False"> <!-- 播放器页面内容 --> </ContentPage>
内容的提问来源于stack exchange,提问作者Pratiksha Pandit
相关产品推荐
相关产品推荐

