winui3开发笔记(二)自定义标题栏

本文介绍了如何在Windows11中使用WindowsAppSDK1.2及以上版本定制App窗口标题栏的颜色,强调了早期版本(如Win10的1.0)不支持此功能。开发者需确保开发环境正确以实现预期效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考文章链接:https://www.programminghunter.com/article/46392310600/

注意事项

获取 AppWindowTitleBar 的实例并设置其颜色属性时,InitializeTitleBar(AppWindow.TitleBar);,只适用于Windows App SDK 1.2及以上,所以如果用win10开发,最多1.0,之前一直没有发现时开发系统环境问题,根本运行不了。所以改成win11开发就可以了
官方文档说明链接:https://learn.microsoft.com/zh-cn/windows/apps/develop/title-bar?tabs=wasdk

代码:

App.xaml.cs:

using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using WinRT.Interop;

namespace App1
{
    public partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
        }
        protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
        {
            m_window = new MainWindow();
            _windowHandle = WindowNative.GetWindowHandle(m_window);
            var windowId = Win32Interop.GetWindowIdFromWindow(_windowHandle);
            AppWindow = AppWindow.GetFromWindowId(windowId);
            AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
            InitializeTitleBar(AppWindow.TitleBar);
            m_window.Activate();
        }

        private IntPtr _windowHandle;
        public static AppWindow AppWindow { get; private set; }// 应用窗口对象.
        public static Window m_window { get; private set; }// 主窗口.
        public static void InitializeTitleBar(AppWindowTitleBar bar)
        {
            bar.ExtendsContentIntoTitleBar = true;
            // 设置成自己预期的颜色即可
            bar.ButtonBackgroundColor = ColorHelper.FromArgb(0, 240, 243, 249);
            bar.ButtonForegroundColor = Colors.Black;
            bar.ButtonHoverBackgroundColor = ColorHelper.FromArgb(0, 240, 243, 249);
            bar.ButtonHoverForegroundColor = Colors.Black;
            bar.ButtonPressedBackgroundColor = ColorHelper.FromArgb(0, 240, 243, 249);
            bar.ButtonPressedForegroundColor = Colors.Black;
            bar.ButtonInactiveBackgroundColor = ColorHelper.FromArgb(0, 240, 243, 249);
            bar.ButtonInactiveForegroundColor = Colors.Black;
        }
    }
}

AppTitleBar.xaml

<Grid Height="48" Padding="16,0,0,0" Background="#F0F3F9" RequestedTheme="Light">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition x:Name="SearchColumn" Width="*" />
        <ColumnDefinition Width="120" />
    </Grid.ColumnDefinitions>

    <StackPanel VerticalAlignment="Center" Orientation="Horizontal" Spacing="16">
        <TextBlock VerticalAlignment="Center" Style="{StaticResource CaptionTextBlockStyle}"
    Text="DeskTop" />
    </StackPanel>
</Grid>

MainWindow.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <!--删除border,改换titlebar-->
    <local1:AppTitleBar />
    <Frame x:Name="frame" Grid.Row="1"></Frame>
</Grid>

按照最开始的文章链接做的实验图片:
1、在这里插入图片描述
2、在这里插入图片描述3、在这里插入图片描述4、自定义
在这里插入图片描述

### 实现 WinUI3 应用程序标题栏中的返回按钮 在 WinUI 3 中,可以通过自定义 `TitleBar` 来添加返回按钮。这涉及到几个关键步骤: #### 设置可扩展的 TitleBar 首先,在 XAML 文件中启用应用程序窗口的自定义标题栏支持。为此,需要获取系统的标题栏并将其设置为可扩展模式。 ```xml <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ...> <!-- 启用自定义标题栏 --> <Window.ExtendsContentIntoTitleBar> True </Window.ExtendsContentIntoTitleBar> <Grid> ... </Grid> </Window> ``` #### 添加返回按钮到 TitleBar 接着,在页面顶部放置一个按钮作为返回按钮,并调整其位置使其位于标题栏区域。还需要编写相应的点击事件处理器逻辑以便响应用户的交互操作。 ```xml <!-- 定义返回按钮 --> <Button Name="BackButton" Click="GoBack_Click"> <FontIcon Glyph=""/> </Button> <!-- 将按钮定位在左上角 --> <Viewbox Width="48" Height="48" VerticalAlignment="Top" HorizontalAlignment="Left"> <StackPanel Orientation="Horizontal"> <Button Style="{StaticResource CaptionButtonStyle}" /> <TextBlock Text="我的应用" FontSize="16" Margin="8,0,0,0"/> </StackPanel> </Viewbox> ``` #### 处理返回按钮点击事件 最后一步是在代码隐藏文件 (`.cs`) 中实现当用户点击返回按钮时触发的动作。通常情况下,这意味着导航回前一页或关闭当前页面视图。 ```csharp private void GoBack_Click(object sender, RoutedEventArgs e) { if (this.Frame.CanGoBack) { this.Frame.GoBack(); } } ``` 以上方法展示了如何利用 WinUI 3 提供的功能来自定义应用程序窗口的外观和行为,从而实现在标题栏内集成返回按钮的效果[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值