四、WinUI3下TitleBar的自定義

来源:https://www.cnblogs.com/duwenlong/archive/2022/10/16/16797668.html
-Advertisement-
Play Games

首先依賴註入 懶得下載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下會很好看。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • explicit的意義是讓程式員能制止"單一參數的constructor"被當作conversion運算符 default constructor default constructor只有在被編譯器需要時,才會被合成出來,且合成出的constructor只執行編譯器所需要的行動(並不對成員初始化) ...
  • ##MybatisPlus生成主鍵策略方法 ###全局id生成策略【因為是全局id所以不推薦】 SpringBoot集成Mybatis-Plus 在yaml配置文件中添加MP配置 mybatis-plus: global-config: db-config: #主鍵類型(auto:"自增id",as ...
  • 首先聲明,本文參照(7條消息) 【中文】【吳恩達課後編程作業】Course 1 - 神經網路和深度學習 - 第三周作業_何寬的博客-CSDN博客_吳恩達課後編程作業(https://blog.csdn.net/u013733326/article/details/79702148) 本文所使用的資料 ...
  • 上一章講到利用路由器鏡像的功能轉發消息,本章介紹物聯網終端的另一應用場景——通過智能終端收發QQ消息。 硬體準備 (無) 環境搭建 實現QQ消息轉發需要依賴社區維護的QQ客戶端gocqhttp以及聊天機器人框架nonebot2,而在這個社區內fubuki-iot是作為一個插件的形式存在的。因此完整的 ...
  • 在上一篇文章`《驅動開發:內核枚舉DpcTimer定時器》`中我們通過枚舉特征碼的方式找到了`DPC`定時器基址並輸出了內核中存在的定時器列表,本章將學習如何通過特征碼定位的方式尋找`Windows 10`系統下麵的`PspCidTable`內核句柄表地址。 ...
  • 1.字元串capitalize函數 (capitalize vt. 資本化,用大寫字母書寫(或印刷); 把…首字母大寫;) 將字元串的首字母大寫,其它字母小寫; 用法:newstr = string.capitalize() 修改後生成一個新字元串(因為字元串是不可更改數據類型); ''.capit ...
  • 大家好,我是陶朱公Boy,又和大家見面了。 前言 在文章開始前,想先問大家一個問題,大家平時在項目需求評審完後,是直接開始編碼了呢?還是會先寫詳細設計文檔,後再開始進行編碼開發? ☆現實 這個時候可能有部分小伙伴會出來反駁:還詳細設計呢連給開發的時間都不夠,項目經常被倒排期。 作者其實能感同身受上述 ...
  • 學會了技術就要使用,否則很容易忘記,因為自然界壓根就不存在什麼代碼、變數之類的玩意,這都是一些和生活常識格格不入的東西。只能多用多練,形成肌肉記憶才行。 在一次實際的產品開發中,由於業務需求的緣故,需要使用Elasticsearch搜索引擎。搜索引擎是通過索引和文檔檢索數據的,索引類似於MySQL的 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...