使用.NET MAUI自定义Windows 10标题栏

问题描述 投票:0回答:1

我找到了这个关于如何使用 WinUI 修改 Windows 10 和 11 中的标题栏的优秀资源:

如何在WinUI 3中更改窗口标题栏颜色

不幸的是,当使用 MAUI 时,我无法应用针对 Windows 10 描述的方法。方法

SetTitleBar(AppTitleBar);
需要来自 WinUI 框架的
Microsoft.UI.Xaml.UIElement
对象,但由于我使用 MAUI,所以我依赖于一组不同的可视组件.

文档没有提及 MAUI。那么,在使用 .NET MAUI 时,如何获得自定义 Windows 10 标题栏的完全访问权限呢?

.net windows-10 maui
1个回答
0
投票

来自文档:https://learn.microsoft.com/en-us/windows/apps/develop/title-bar#colors

当应用程序在 Windows 10 上运行时,颜色自定义将被忽略。

因此您可以尝试另一种解决方案来更改标题栏的背景颜色。您可以打开

Platforms\Windows
文件夹中的App.xaml,在maui:MauiWinUIApplication标签中添加以下代码,标题栏的背景颜色可以通过Border控件中的Background改变,也可以通过TextBlock自定义标题:

<maui:MauiWinUIApplication.Resources>

        <DataTemplate x:Key="MauiAppTitleBarTemplate">

            <Grid >

                <Border

                  Canvas.ZIndex="1"

                  VerticalAlignment="Stretch"

                  Background="Azure"

                  Margin="0,0,0,0">

                    <TextBlock

                      Foreground="Red"

                      VerticalAlignment="Center"

                      HorizontalAlignment="Center"

                      Text="Custom your  application title"

                      FontWeight="Bold"/>

                </Border>
            </Grid>
        </DataTemplate>
</maui:MauiWinUIApplication.Resources>

输出:

© www.soinside.com 2019 - 2024. All rights reserved.