MAUI Windows 自定义标题栏

MAUI Windows 自定义标题栏

你需要定位到文件:App.xaml ,注意是 Windows 平台特化的 App.xaml。
在这里插入图片描述

翻了 github 好久才找到解决方法。参考项目: 点此查看 GitHub 项目页

MAUI 的 Windows 底层是直接搬 WinUI 3.0 的,但是 XAML 编辑器默认只是装载 Android 的程序集,所以你会遇到这样的问题——找不到相关类型:
在这里插入图片描述
在这里插入图片描述
所以在这里写其实是非常折磨的,所以你可以换用在 WPF 或者是 WinUI 3.0 的原生环境写这个。

一、主要代码

注意:这个样式资源的 Key 是写死的,原理是覆盖原来的同名资源 Key,虽然不怎么优雅,但是比 SetTitleBar() 微软文档提到的这种方式来说,更为方便,比较好实现,因为只用改这一块即可。

在此之前请在 App.xaml 里面加上资源,因为没有智能提示,为此这边已经给你把东西准备好了。

<maui:MauiWinUIApplication.Resources>
    <ResourceDictionary>



        <!--  样式放这里  -->



    </ResourceDictionary>
</maui:MauiWinUIApplication.Resources> 

然后将下文样式嵌入进去

<Style x:Key="WindowChromeStyle" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Grid x:Name="LayoutRoot" Background="#512bdf">
                    <ContentPresenter
                    x:Name="ClientAreaPresenter"
                    Content="{TemplateBinding Content}"
                    ContentTemplate="{TemplateBinding ContentTemplate}"
                    ContentTransitions="{TemplateBinding ContentTransitions}"
                    Foreground="{TemplateBinding Foreground}" />

                    <Grid
                    x:Name="TitleBarMinMaxCloseContainer"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top"
                    Visibility="{Binding CaptionVisibility, RelativeSource={RelativeSource TemplatedParent}}">
                        <Grid
                        x:Name="MinMaxCloseContainer"
                        HorizontalAlignment="Right"
                        VerticalAlignment="Top"
                        ColumnDefinitions="Auto,Auto,Auto">

                            <!--  最小化  -->
                            <Button
                            x:Name="MinimizeButton"
                            Grid.Column="0"
                            AutomationProperties.AutomationId="_MinimizeButton"
                            AutomationProperties.Name="Minimize"
                            Content="M 0 0 H 10"
                            Style=
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值