如何在UWP中将图标设置为TitleBar(Window)?
TitleBar图标的示例:
我们可以自定义标题栏以设置TitleBar图标。这里的重点是使用Window.SetTitleBar方法。以下是一个简单的示例:
首先,我们需要一个UIElement作为新的标题栏。例如,在MainPage.xaml中,我们可以Grid
在网格中添加和,设置图标和应用程序名称。请注意,我们需要将“ 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上的官方标题栏示例,特别是场景2:示例中的自定义工程图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句