【问题标题】:How to set TitleBar Icon in UWP?如何在 UWP 中设置 TitleBar 图标?
【发布时间】:2016-11-15 09:28:05
【问题描述】:

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

标题栏图标示例:

【问题讨论】:

    标签: c# uwp titlebar uwp-xaml


    【解决方案1】:

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

    首先,我们需要一个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上的官方Title bar sample,特别是场景2:示例中的自定义绘图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 2021-04-05
      • 2020-10-10
      相关资源
      最近更新 更多