如何在UWP WinUI 2应用的标题栏中显示TabView?
在UWP WinUI 2中实现TabView嵌入标题栏
1. 配置窗口标题栏(后台代码)
需要让应用窗口内容扩展到标题栏区域,并将标题栏按钮背景设为透明,避免遮挡TabView。在MainPage.xaml.cs的构造函数中添加以下代码:
using Windows.UI.ViewManagement; using Windows.UI; public MainPage() { this.InitializeComponent(); // 扩展窗口内容到标题栏 CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true; // 设置标题栏按钮背景为透明 ApplicationViewTitleBar titleBar = ApplicationView.GetForCurrentView().TitleBar; titleBar.ButtonBackgroundColor = Colors.Transparent; titleBar.ButtonInactiveBackgroundColor = Colors.Transparent; }
2. 调整XAML布局,将TabView放置到标题栏区域
修改布局结构,把TabView放在对应标题栏的行,并设置右侧Padding避开窗口的最小化/最大化/关闭按钮:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <!-- 适配标题栏高度 --> <RowDefinition Height="*"/> <!-- 主内容区域 --> </Grid.RowDefinitions> <!-- 嵌入标题栏的TabView --> <muxc:TabView Grid.Row="0" Padding="12,0,140,0"> <!-- 右侧预留140px给窗口按钮 --> <muxc:TabViewItem Header="New Tab"> <!-- 标签页内容 --> </muxc:TabViewItem> </muxc:TabView> <!-- 主内容容器 --> <Frame Grid.Row="1"> <!-- 页面主体内容 --> </Frame> </Grid>
3. 恢复窗口拖拽功能
默认情况下,标题栏被覆盖后无法拖拽窗口,需要给TabView的空白区域添加拖拽支持:
在XAML中给TabView绑定PointerPressed事件:
<muxc:TabView Grid.Row="0" Padding="12,0,140,0" PointerPressed="TabView_PointerPressed"> <!-- 标签项 --> </muxc:TabView>
然后在后台实现事件逻辑:
private void TabView_PointerPressed(object sender, PointerRoutedEventArgs e) { var tabView = sender as Microsoft.UI.Xaml.Controls.TabView; var hitElement = tabView.InputHitTest(e.GetCurrentPoint(tabView).Position); // 仅在点击TabView空白区域时触发拖拽 if (!(hitElement is Microsoft.UI.Xaml.Controls.TabViewItem)) { Window.Current.CoreWindow.DragMove(); } }
内容的提问来源于stack exchange,提问作者Alper Akca
相关产品推荐
相关产品推荐

