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=