如何在UWP中设置TitleBar图标?

ina*_*off 6 c# titlebar uwp uwp-xaml

如何在UWP中将图标设置为TitleBar(Window)?

TitleBar图标示例:

Jay*_*Zuo 9

我们可以自定义标题栏来设置TitleBar Icon.这里的关键点是使用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>
Run Code Online (Sandbox Code Playgroud)

然后在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);
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅GitHub上的官方标题栏示例,尤其是场景2:示例中的自定义绘图.