首先依賴註入 懶得下載autofac了 直接用程式集進行批量註入 private static WebApplicationBuilder builder; internal static void Load(WebApplicationBuilder web) { builder = web; b ...
WinUI3下TitleBar的自定義
對於Windows軟體開發者來說重寫標題欄樣式是一個很重要的事情,在WPF階段很多人寫出來性能很差的視窗,而且為了適配Win11系統的Snaplayout後性能就更差了,這篇是寫WinUI3下提供的重寫TitleBar的方式;
1、修改文本
public MainWindow()
{
this.InitializeComponent();
Title = "Duwenlong learn App Title";
}
可以修改預設標題欄顯示文本;但是無法自定義其他內容;所有操作都請在InitializeComponent方法後執行,不然會報錯;
public MainWindow()
{
this.InitializeComponent();
// Hide default title bar.
ExtendsContentIntoTitleBar = true;
}
修改後我們有完整的區域用看顯示內容,只是目前還比較醜而且標題欄部分不支持拖動;
2、完全自定義
通過調用 Window.SetTitleBar 方法並傳入定義拖動區域的 UIElement 來指定拖動區域。
1、標題欄內容和拖動區域
代碼如下:
<Window
x:Class="TitleBarCustomizationDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TitleBarCustomizationDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="AppTitleBar">
<TextBlock x:Name="AppTitleTextBlock" Text="Duwenlong learn Now App Title"
TextWrapping="NoWrap"
Style="{StaticResource CaptionTextBlockStyle}"
VerticalAlignment="Center"
Margin="28,6,0,6"/>
</Grid>
</Grid>
</Window>
public MainWindow()
{
this.InitializeComponent();
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
}
2、互動式內容
我們可以放置一些不影響用戶的互動式按鈕
使用XAML的嵌套語法嘗試放置一些內容,我放置了一個textblock,用於在標題欄顯示一個消息;
<Window
x:Class="TitleBarCustomizationDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TitleBarCustomizationDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="AppTitleBar" >
<TextBlock x:Name="AppTitleTextBlock" Text="Duwenlong learn Now App Title"
TextWrapping="NoWrap" Margin="28,6,0,6"
Style="{StaticResource CaptionTextBlockStyle}"
VerticalAlignment="Center"
/>
<TextBlock Margin="28,6,0,6" Text="1 problem to be fixed" Width="220"/>
</Grid>
</Grid>
</Window>
4、系統標題按鈕的顏色和透明度
將應用內容擴展到標題欄區域時,可以使標題按鈕的背景透明,我預設設置了WindowCaptionBackground為透明色
<Application
x:Class="TitleBarCustomizationDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TitleBarCustomizationDemo">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
<SolidColorBrush x:Key="WindowCaptionBackground">Transparent</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">LightGreen</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionForeground">Red</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionForegroundDisabled">Pink</SolidColorBrush>
</ResourceDictionary>
</Application.Resources>
</Application>
5、當視窗處於非活動狀態時,調暗標題欄
<Application
x:Class="TitleBarCustomizationDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TitleBarCustomizationDemo">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
<!-- <SolidColorBrush x:Key="WindowCaptionBackground">Transparent</SolidColorBrush> -->
<SolidColorBrush x:Key="WindowCaptionBackground">Green</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">LightGreen</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionForeground">Red</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionForegroundDisabled">Pink</SolidColorBrush>
</ResourceDictionary>
</Application.Resources>
</Application>
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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace TitleBarCustomizationDemo
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
Activated += MainWindow_Activated;
}
private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
{
if (args.WindowActivationState == WindowActivationState.Deactivated)
{
AppTitleTextBlock.Foreground =
(SolidColorBrush)App.Current.Resources["WindowCaptionForegroundDisabled"];
}
else
{
AppTitleTextBlock.Foreground =
(SolidColorBrush)App.Current.Resources["WindowCaptionForeground"];
}
}
}
}
6、重置標題欄
可以在應用運行時調用 SetTitleBar 切換到新的標題欄元素。或者恢復為預設;
<Window
x:Class="TitleBarCustomizationDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TitleBarCustomizationDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="AppTitleBar" >
<TextBlock x:Name="AppTitleTextBlock" Text="Duwenlong learn Now App Title"
TextWrapping="NoWrap" Margin="28,6,0,6"
Style="{StaticResource CaptionTextBlockStyle}"
VerticalAlignment="Center"
/>
<TextBlock Margin="28,6,0,6" Text="1 problem to be fixed" Width="220"/>
</Grid>
<Button Grid.Row="1" Content="Click Me" Click="Button_Click"/>
</Grid>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
SetTitleBar(null);
ExtendsContentIntoTitleBar = false;
}
因為使用的是Win10機器,圖床又掛了所以不貼圖了,Win11下會很好看。