简体   繁体   English

如何在标题栏中查看 TabView? 在 UWP WinUI 2 应用程序中

[英]How do I view TabView in Title Bar? In UWP WinUI 2 Application

I want to view TabView in title bar like image below.我想在标题栏中查看 TabView,如下图所示。 在此处输入图像描述 I'm using UWP WinUI 2.我正在使用 UWP WinUI 2。

I'm trying to view my TabView in title bar.我正在尝试在标题栏中查看我的 TabView。 But TabView is viewing under title bar.但是 TabView 正在标题栏下查看。

My MainPage.xaml code:我的 MainPage.xaml 代码:

<muxc:TabView Grid.Row="2">
    <muxc:TabViewItem Header="New Tab">
                
    </muxc:TabViewItem>
</muxc:TabView>

This is actually pretty easy:这实际上很简单:

In your xaml code: This piece of code adds a ShellTitlebarInset and a CustomDragRegion to the TabView.在您的 xaml 代码中:这段代码向 TabView 添加了一个 ShellTitlebarInset 和一个 CustomDragRegion。 This is needed, to add a margin to the left and right side of the window.这是需要的,以便在 window 的左侧和右侧添加边距。

<muxc:TabView x:Name="tabView">
    <muxc:TabView.TabStripHeader>
        <Grid x:Name="ShellTitlebarInset" Background="Transparent" />
    </muxc:TabView.TabStripHeader>
    <muxc:TabView.TabStripFooter>
        <Grid x:Name="CustomDragRegion" MinWidth="188" Loaded="CustomDragRegion_Loaded" Background="Transparent" />
    </muxc:TabView.TabStripFooter>
    
    <muxc:TabViewItem Header="Tab1"/>
    <muxc:TabViewItem Header="Tab2"/>
    <muxc:TabViewItem Header="Tab3"/>
</muxc:TabView>

In your MainPage:在您的主页中:

The LayoutMetricsChanged event handles the FlowDirection either from LeftToRight or RightToLeft to add the specific margin to the CustomDragRegion and ShellTitlebarInset. LayoutMetricsChanged 事件处理来自 LeftToRight 或 RightToLeft 的 FlowDirection,以将特定边距添加到 CustomDragRegion 和 ShellTitlebarInset。

private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
    if (FlowDirection == FlowDirection.LeftToRight)
    {
        CustomDragRegion.MinWidth = sender.SystemOverlayRightInset;
        ShellTitlebarInset.MinWidth = sender.SystemOverlayLeftInset;
    }
    else
    {
        CustomDragRegion.MinWidth = sender.SystemOverlayLeftInset;
        ShellTitlebarInset.MinWidth = sender.SystemOverlayRightInset;
    }

    CustomDragRegion.Height = ShellTitlebarInset.Height = sender.Height;
}


//Make sure to extend the view after the CustomDragRegion loaded, otherwise the tabs may clip under the minimize, maximize and close buttons of the window:
private void CustomDragRegion_Loaded(object sender, RoutedEventArgs e)
{
    var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
    coreTitleBar.ExtendViewIntoTitleBar = true;
    coreTitleBar.LayoutMetricsChanged += CoreTitleBar_LayoutMetricsChanged;

    Window.Current.SetTitleBar(CustomDragRegion);
}

Here also the official documentation from Microsoft:这里还有微软的官方文档:

https://learn.microsoft.com/en-us/windows/apps/design/controls/tab-view https://learn.microsoft.com/en-us/windows/apps/design/controls/tab-view

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

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