如何在UWP中将图标设置为TitleBar(窗口)?
TitleBar图标示例:
答案 0 :(得分:9)
我们可以自定义标题栏以设置TitleBar图标。这里的关键点是使用Window.SetTitleBar method。以下是一个简单的示例:
首先,我们需要UIElement作为新标题栏。例如,在 MainPage.xaml 中,我们可以添加Grid
并在网格中设置图标和应用程序名称。请注意,我们需要把#34; TitleBar"根网格的第一行中的Grid
。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid x:Name="TitleBar">
<Rectangle x:Name="BackgroundElement" Fill="Transparent" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Height="32" Margin="5,0" Source="Assets/StoreLogo.png" />
<TextBlock Grid.Column="1" VerticalAlignment="Center" Text="My Application" />
</Grid>
</Grid>
</Grid>
然后在 MainPage.xaml.cs 中,我们可以使用以下代码设置带有图标的新标题栏。
public MainPage()
{
this.InitializeComponent();
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
// Set the BackgroundElement instead of the entire Titlebar grid
// so that we can add clickable element in title bar.
Window.Current.SetTitleBar(BackgroundElement);
}
有关详情,请参阅GitHub上的官方Title bar sample,特别是方案2:示例中的自定义绘图。