簡體   English   中英

如何在UWP中設置TitleBar圖標?

[英]How to set TitleBar Icon in UWP?

如何在UWP中將圖標設置為TitleBar(Window)?

TitleBar圖標示例:

我們可以自定義標題欄來設置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>

然后在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上的官方標題欄示例 ,尤其是場景2:示例中的自定義繪圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM