我的应用程序需要一个非常简单的用户界面,只有一个文本块和图像(基本上没有交互元素)。 所以我在 App.xaml.cs 中使用以下代码启动应用程序和应用程序激活方法。
private void SetupTitleBar()
{
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ButtonBackgroundColor = Colors.Transparent;
}
这是我在 MainPage.xaml 中的简单标题栏组件
<Grid x:Name="TitleBar" MinHeight="32" Background="{ThemeResource TitleBarAcrylicBackgroundBrush}" Grid.row =0 VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Height="16" Width="16" Margin="16,8,12,8" HorizontalAlignment="Left" VerticalAlignment="Center"
Source="../Assets/AppLogo.png" />
<TextBlock
x:Name="AppNameTextBlock"
Margin="44,8,0,8"
Text="AppName"
FontSize="12"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Grid>
在 MainPage.xaml.cs 中,我有
public MainPage()
{
InitializeComponent();
Window.Current.SetTitleBar(TitleBar)
}
在正常模式下一切正常。当我点击某个按钮切换到紧凑叠加模式时,要求不显示标题栏。 因此在紧凑叠加模式下,我无法使用光标拖动应用程序,因为我将 TitleBar 网格的可见性设置为 false,这是可以理解的,因为我设置为 tilebar 的 UI 元素不可见。
删除行Window.Current.SetTitleBar(TitleBar)主页中的搜索按钮有时无法点击应用程序的左上角。
我尝试了多种从文档设置标题栏的方法,但似乎没有任何效果。请帮忙。提前致谢
在UWP应用中,标题栏是窗口的组成部分,在紧凑叠加模式下无法完全隐藏。但是,您可以通过自定义其外观以匹配背景颜色或降低其高度来降低标题栏的可见度。
要在紧凑叠加模式下自定义标题栏,您可以使用
CoreApplicationViewTitleBar
类。这是一个如何最小化标题栏可见性的示例。
// Get the CoreApplicationViewTitleBar
CoreApplicationViewTitleBar titleBar = CoreApplication.GetCurrentView().TitleBar;
// Set the title bar colors.
titleBar.ExtendViewIntoTitleBar = true;
ApplicationViewTitleBar viewTitleBar = ApplicationView.GetForCurrentView().TitleBar;
viewTitleBar.ButtonBackgroundColor = Colors.Transparent;
viewTitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
// Reduce the height of the title bar
viewTitleBar.Height = 20;` // Set the desired height
// Set the title bar to an accent color (optional)
titleBar.BackgroundColor = Colors.YourDesiredColor;
titleBar.ForegroundColor = Colors.YourDesiredColor;
通过将 ExtendViewIntoTitleBar 设置为 true,您的应用程序的内容将扩展到标题栏区域。然后,您可以将 ButtonBackgroundColor 和 ButtonInactiveBackgroundColor 属性设置为 Colors.Transparent 以使按钮与标题栏混合。此外,您可以通过设置 ApplicationViewTitleBar 的 Height 属性来降低标题栏的高度。
请注意,虽然您可以自定义标题栏的外观,但在 UWP 应用程序中无法以紧凑叠加模式将其完全隐藏。标题栏在某种程度上仍然可见。