加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:27:41 所属栏目:百科 来源:网络整理
导读:如何在UWP中将图标设置为TitleBar(Window)? TitleBar图标示例: 解决方法 我们可以自定义标题栏来设置TitleBar Icon.这里的关键点是使用 Window.SetTitleBar method.以下是一个简单的示例: 首先,我们需要一个UIElement作为新的标题栏.例如,在MainPage.xaml
如何在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:示例中的自定义绘图.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读