我试图设置应用标题栏的应用名称,但它出现在标题栏的底部。
而不是标题作为WinUI桌面在顶部,我想有我的应用程序标题在顶部。另外,如何更改标题栏的背景颜色?
下面是我当前设置标题栏的MainPage.xaml代码
MainPage.xaml
x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProject"
xmlns:controlpages="using:MyProject"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid x:Name="AppTitleBar">
<!-- <Image Source="Images/WindowIcon.png"
HorizontalAlignment="Left"
Width="16" Height="16"
Margin="8,0"/> -->
<TextBlock x:Name="AppTitleTextBlock" Text="App title"
TextWrapping="NoWrap"
Style="{StaticResource CaptionTextBlockStyle}"
VerticalAlignment="Center"
Margin="28,0,0,0"/>
</Grid>
<NavigationView Grid.Row="1" x:Name="nvSample8"
PaneDisplayMode="Left"
IsTabStop="False"
SelectionChanged="NavigationView_SelectionChanged8"
IsPaneOpen="False"
>
<NavigationView.MenuItems>
<NavigationViewItem Content="Accounts" Icon="Contact" ToolTipService.ToolTip="Accounts" Tag="AccountsPage">
</NavigationViewItem>
</NavigationView.MenuItems>
<Frame x:Name="contentFrame8" />
</NavigationView>
</Grid>
</Page>
MainPage.xaml.cs
{
public MainPage()
{
this.InitializeComponent();
nvSample8.SelectedItem = nvSample8.MenuItems.OfType<NavigationViewItem>().First();
AppTitleTextBlock.Text = "My App Title";
}
}
发布于 2022-09-26 10:07:14
@Nick的答案已经显示了您需要检查的正确文档。但是你可能需要检查它的UWP部分。
要更改标题栏的文本和背景,首先需要获取CoreApplicationViewTitleBar并将ExtendViewIntoTitleBar属性设置为true
。然后,您可以传递您创建的作为UIElement的TitleBar。之后,您可以通过获取包含这些按钮的系统标题栏来更改系统标题按钮的颜色。
MainPage.Xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid x:Name="AppTitleBar" Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="LeftPaddingColumn" Width="0"/>
<ColumnDefinition/>
<ColumnDefinition x:Name="RightPaddingColumn" Width="0"/>
</Grid.ColumnDefinitions>
<Image Source="Assets/StoreLogo.png"
Grid.Column="1"
HorizontalAlignment="Left"
Width="16" Height="16"
Margin="8,0,0,0"/>
<TextBlock x:Name="AppTitleTextBlock"
Text="App title"
Style="{StaticResource CaptionTextBlockStyle}"
Grid.Column="1"
VerticalAlignment="Center"
Margin="28,0,0,0"/>
</Grid>
</Grid>
MainPage.Xaml.cs
public MainPage()
{
this.InitializeComponent();
var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = true;
// Set XAML element as a drag region.
Window.Current.SetTitleBar(AppTitleBar);
//change color for the caption buttons
var systemTitleBar = ApplicationView.GetForCurrentView().TitleBar;
systemTitleBar.ButtonBackgroundColor = Colors.Transparent;
systemTitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
systemTitleBar.ButtonPressedBackgroundColor = Colors.Transparent;
systemTitleBar.ButtonHoverBackgroundColor = Colors.Transparent;
}
发布于 2022-09-23 13:23:39
您是否尝试过将Window.ExtendsContentIntoTitleBar
设置为true?
您可能需要检查一下:https://learn.microsoft.com/en-us/windows/apps/develop/title-bar?tabs=winui3
https://stackoverflow.com/questions/73827251
复制相似问题