如何去除UWP应用(XAML)中的白色边框?
解决白色边框及适配880px宽度问题
步骤1:修改Page和Grid的XAML属性
默认Page控件自带内边距,这是白色边框的常见诱因,同时固定宽高会限制内容铺满窗口,调整代码如下:
<Page x:Class="FirstScreen.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:FirstScreen" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="0"> <!-- 移除默认内边距 --> <Grid Background="Black"> <!-- 去掉固定宽高,让Grid自适应Page --> </Grid> </Page>
步骤2:设置应用窗口固定宽度为880px
在App.xaml.cs的OnLaunched方法中添加窗口尺寸配置,确保启动时窗口宽度锁定为880px:
protected override void OnLaunched(LaunchActivatedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { // 可添加状态恢复逻辑 } Window.Current.Content = rootFrame; } if (e.PrelaunchActivated == false) { if (rootFrame.Content == null) { rootFrame.Navigate(typeof(MainPage), e.Arguments); } // 设置窗口最小尺寸为880px宽,同时强制调整窗口宽度 var appView = ApplicationView.GetForCurrentView(); appView.SetPreferredMinSize(new Size(880, 600)); appView.TryResizeView(new Size(880, appView.VisibleBounds.Height)); Window.Current.Activate(); } }
步骤3:排查系统窗口边框(可选)
若仍有白色边框,可能是系统窗口默认样式导致,可将内容延伸至标题栏并统一背景色:
在App.xaml.cs的OnLaunched方法中,Window.Current.Activate()之前添加:
var coreTitleBar = CoreApplication.GetCurrentView().TitleBar; coreTitleBar.ExtendViewIntoTitleBar = true; var titleBar = ApplicationView.GetForCurrentView().TitleBar; titleBar.ButtonBackgroundColor = Colors.Black; titleBar.ButtonHoverBackgroundColor = Colors.DarkGray;
完成以上调整后,应用会适配880px宽度并移除白色边框,内容铺满窗口。
内容的提问来源于stack exchange,提问作者Егор Кротенко
相关产品推荐
相关产品推荐

