c# - UWP 标题栏按钮设置为透明时呈白色

标签 c# xaml uwp

Windows 10 Build 18362。不确定 Windows 是否存在错误,但仍在寻找解决方案。

我认为我有一个复杂的问题,需要一个过程来重现。最终发生的事情是这样的:

enter image description here

该应用程序使用系统标题栏(不是扩展到标题栏的 View ),并且在我的计算机上,禁用了标题栏中允许强调颜色的系统设置。这就是为什么标题按钮是蓝色(系统强调色)但实际标题栏是黑色的。我所期望的以及应该发生的情况(正如我将证明的那样)是标题按钮(最小化、最大化、关闭)应该是黑色的,就像标题栏的其余部分一样。

我尝试解决该问题的第一件事(当我第一次在实际应用程序中注意到这一点时)是将按钮的背景颜色设置为透明。发生的情况是这样的:

enter image description here

全白!好的,现在来看一个可重现的示例。

  • 创建一个新的 UWP 应用程序。我使用了 C#,不确定这是否重要,但为了完整起见,我建议您也这样做。
  • 确保在系统设置中禁用标题栏中的强调色
  • 运行默认应用程序并注意标题栏的行为。 这是预期的行为,因为标题按钮是黑色的(在深色主题中)。这就是为什么我相信我所经历的事情是无意的
  • 我向 XAML 页面添加了 Acrylic 效果,因为我认为这就是问题所在。似乎并非如此,但为了完整起见,请使用以下 XAML 和 C#(当然,将 App1 更改为您的应用程序名称):
<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource SystemControlAcrylicWindowMediumHighBrush}">

    <Grid>

    </Grid>
</Page>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            ApplicationViewTitleBar formattableTitleBar = ApplicationView.GetForCurrentView().TitleBar;
            CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = false;
            formattableTitleBar.ButtonBackgroundColor = Colors.Transparent;
            formattableTitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
        }
    }
}

您现在应该可以看到我在第二个屏幕截图中看到的内容。如果您注释掉 MainPage.xaml.cs 中将按钮颜色分配为透明的两行,您将得到我在第一个屏幕截图中看到的内容。

我错过了什么吗?这是预期的行为吗?如何使标题按钮始终遵循规则?

最佳答案

我理解你的问题,这种行为确实令人困惑。

标题栏有一个颜色层,就像我们穿的衣服一样。你可以随意改变衣服的颜色,但你无法改变人的颜色。

标题栏本身是白色的。当您将 ButtonBackgroundColor 设置为“透明”时,颜色层实际上被设置为透明,显示标题栏的原始颜色。

由于您当前是深色主题,因此 ButtonForegroundColor 为白色,这会导致您看到白色背景和白色文本。如果你将TitleBar的TitleColor设置为Transparent,你就会明白为什么TitleBar有颜色层了。

关于c# - UWP 标题栏按钮设置为透明时呈白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58275136/

相关文章:

c# - 带有 if(array[i + 1]) 的 For 循环超出数组边界;如何循环到开头?

c# - 使用 C# 将内联 XML 节点转换为 asp.net 中的嵌套节点

c# - 如何从类中获取 'ReadOnly' 或 'WriteOnly' 属性?

c# - 如何在 XAML 中处理 "float"图像 (Windows 8 Metro)

c# - 如何从 xamarin 表单中的绑定(bind)命令调用函数

winapi - 如何在 AppxManifest.xml 文件中为使用 Desktop Bridge 转换的 Win32 应用程序正确构建 UWP 应用程序图标(Project Centennial)

c# - Microsoft.NET.CoreRuntime 的 SOS 调试扩展

c# - 首次启动应用程序时如何显示页面

c# - 使用 Style 将 Calendar 的宽度更改为其父 DatePicker 的宽度

c# - [通用 Windows 平台] 中的 System.Net.Http.HttpClient 无法正常工作