如何在UWP中设置标题栏图标?

11

如何在UWP中设置标题栏(窗口)图标?

标题栏图标示例:

1个回答

10

我们可以自定义标题栏来设置标题栏图标,关键点是使用Window.SetTitleBar方法。以下是一个简单示例:

首先,我们需要一个UIElement作为新的标题栏。例如,在MainPage.xaml中,我们可以添加一个Grid,并在该网格中设置图标和应用程序名称。请注意,我们需要将“TitleBar”Grid放置在根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上官方的标题栏示例,特别是示例中的第二个场景:自定义绘图


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接