在UWP app c#中使用ExtendViewIntoTitleBar = true无法看到标题栏

时间:2019-07-15 12:44:25

标签: c# windows uwp

因此,我想根据此处https://docs.microsoft.com/en-gb/windows/uwp/design/shell/title-bar的建议来自定义UWP应用的标题栏。我想使用自定义Windows.UI.Xaml.Controls.NavigationView并显示它而不是默认标题栏。所以在我的Xaml代码中,我有:

<Page
    x:Class="Posta.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Posta"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <NavigationView x:Name="TopNavigationView" Header="Hello" PaneDisplayMode="Top" IsBackButtonVisible="Collapsed" Grid.Row="0">
                <NavigationView.MenuItems>
                    <NavigationViewItem x:Name="HomeItem" Content="Home" Icon="Home" ></NavigationViewItem>
                </NavigationView.MenuItems>
            </NavigationView>
        <WebView Source="https://website.com" Grid.Row="1" ></WebView>
    </Grid>
</Page>

并在初始化页面时在我的C#代码中添加了

    var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
    coreTitleBar.ExtendViewIntoTitleBar = true;

    TopNavigationView.Height = coreTitleBar.Height; // check this height setting (because we sat it to auto in xaml)
    Window.Current.SetTitleBar(TopNavigationView);

但是,现在当我启动该应用程序时,我的NavigationView不可见,并且只有WebView扩展到了标题栏中。

1 个答案:

答案 0 :(得分:0)

您需要处理CoreApplicationViewTitleBar.LayoutMetricsChanged Event才能将TopNavigationView.Height更新为coreTitleBar.Height,就像演示代码中显示的文档一样。

private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
    UpdateTitleBarLayout(sender);
}

private void UpdateTitleBarLayout(CoreApplicationViewTitleBar coreTitleBar)
{
    // Update title bar control size as needed to account for system size changes.
    TopNavigationView.Height = coreTitleBar.Height;
}