微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在UWP中设置TitleBar图标?

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

TitleBar图标示例:

解决方法

我们可以自定义标题栏来设置TitleBar Icon.这里的关键点是使用 Window.SetTitleBar method.以下是一个简单的示例:

首先,我们需要一个UIElement作为新的标题栏.例如,在MainPage.xaml中我们可以添加一个Grid,并在网格中设置图标和应用程序名称.请注意,我们需要将“TitleBar”网格放在根网格的第一行.

<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:示例中的自定义绘图.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐