xamlwinrt-xamlwinui-3

WinUI3: Why does the top area of the NavigationView look different?


The default appearance of the NavigationView in WinUI3 Gallery or an app created with a template studio has a space at the top. However, it looks different in apps created with Visual Studio default templates.I don't think it's controlled by the ViewModel or anything else. Why does it look different?

<!--In Template studio or WinUI3 Gallery-->
<Page>
    <Grid>
        <NavigationView PaneDisplayMode="LeftCompact"/>
    </Grid>
</Page>
<!--In My App created with Visual Studio default templates-->
<Page>
    <Grid>
        <NavigationView PaneDisplayMode="LeftCompact"/>
    </Grid>
</Page>

In Template studio or WinUI3 Gallery

In My App created with Visual Studio default templates

Even if you modify the ShellPage of an app created with a Template Studio as follows, there is still a difference in appearance.

public sealed partial class ShellPage : Page
{
    public ShellPage()
    {
        InitializeComponent();
    }
}
<Page
    x:Class="TemplateStudioApp.Views.ShellPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    
    <NavigationView PaneDisplayMode="LeftCompact"/>
</Page>

Solution

  • That space at the top is actually the AppTitleBar. This code should create the same look.

    App.xaml

    <Application
        x:Class="WinUI3App.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:WinUI3App">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                    <!--  Other merged dictionaries here  -->
                </ResourceDictionary.MergedDictionaries>
                <!--  Other app resources here  -->
                <SolidColorBrush
                    x:Key="WindowCaptionBackground"
                    Color="Transparent" />
                <SolidColorBrush
                    x:Key="WindowCaptionBackgroundDisabled"
                    Color="Transparent" />
                <Thickness x:Key="NavigationViewContentMargin">0,48,0,0</Thickness>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    MainWindow.xaml.cs

    <Window
        x:Class="WinUI3App.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="using:WinUI3App"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Grid>
            <Grid
                x:Name="AppTitleBar"
                Height="{Binding ElementName=NavigationViewControl, Path=CompactPaneLength}"
                VerticalAlignment="Top"
                Canvas.ZIndex="1"
                IsHitTestVisible="True">
                <Image
                    Width="16"
                    Height="16"
                    HorizontalAlignment="Left"
                    Source="/Assets/WindowIcon.ico" />
                <TextBlock
                    x:Name="AppTitleBarText"
                    Margin="28,0,0,0"
                    VerticalAlignment="Center"
                    Style="{StaticResource CaptionTextBlockStyle}"
                    TextWrapping="NoWrap" />
            </Grid>
            <NavigationView
                x:Name="NavigationViewControl"
                Canvas.ZIndex="0"
                DisplayModeChanged="NavigationView_DisplayModeChanged"
                ExpandedModeThresholdWidth="1280"
                IsBackButtonVisible="Visible"
                IsSettingsVisible="True" />
        </Grid>
    </Window>
    

    MainWindow.xaml.cs

    using Microsoft.UI.Xaml;
    using Microsoft.UI.Xaml.Controls;
    
    namespace WinUI3App;
    
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
    
            ExtendsContentIntoTitleBar = true;
            SetTitleBar(this.AppTitleBar);
        }
    
        private void NavigationView_DisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args)
        {
            AppTitleBar.Margin = new Thickness()
            {
                Left = sender.CompactPaneLength * (sender.DisplayMode == NavigationViewDisplayMode.Minimal ? 2 : 1),
                Top = AppTitleBar.Margin.Top,
                Right = AppTitleBar.Margin.Right,
                Bottom = AppTitleBar.Margin.Bottom
            };
        }
    }