简体   繁体   English

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

[英]Title bar not visible using ExtendViewIntoTitleBar=true in UWP app c#

So I would like to customize the title bar of my UWP app as suggested here https://docs.microsoft.com/en-gb/windows/uwp/design/shell/title-bar . 因此,我想按照此处的建议来自定义UWP应用程序的标题栏https://docs.microsoft.com/en-gb/windows/uwp/design/shell/title-bar I would like to use a custom Windows.UI.Xaml.Controls.NavigationView and display that instead of the default title bar. 我想使用自定义Windows.UI.Xaml.Controls.NavigationView并显示它,而不是默认标题栏。 So in my Xaml code I have: 所以在我的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>

And in my C# code I added when initializing the 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);

However, now my NavigationView is not visible when I launch the application and only the WebView is extended into the titlebar. 但是,现在,当我启动应用程序时,我的NavigationView不可见,并且只有WebView扩展到了标题栏中。

You need to handle the CoreApplicationViewTitleBar.LayoutMetricsChanged Event to update the TopNavigationView.Height by coreTitleBar.Height like the document has shown in the demo code. 您需要处理CoreApplicationViewTitleBar.LayoutMetricsChanged事件以通过coreTitleBar.Height更新TopNavigationView.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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM