简体   繁体   English

WinUI 3 无法正确更改标题栏的背景颜色

[英]WinUI 3 Can't properly change the background colour of the titlebar

My aim is to make the background colour of the title bar and the content inside the app to be the same colour, but that is not working.我的目标是使标题栏的背景颜色和应用程序内的内容颜色相同,但这是行不通的。

I wanted to make everything of this colour #FF2B2B2B, the problem is while the content of the app is of the correct colour, the colour of the title bar is lighter than it should be, in fact the same issue happens with the default XAML colour Black but everything works fine with the default XAML colour Red.我想把所有东西都变成这种颜色#FF2B2B2B,问题是虽然应用程序的内容颜色正确,但标题栏的颜色比应该的颜色浅,事实上默认的 XAML 颜色也会出现同样的问题黑色,但使用默认的 XAML 红色一切正常。

MainWindow.xaml主窗口.xaml

<Window
x:Class="TItleBarBackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TItleBarBackground"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>
    <Grid.Resources>
        <SolidColorBrush x:Key="Colour" Color="#FF2B2B2B"/>
    </Grid.Resources>
    <Grid x:Name="AppTitleBar" Height="28" HorizontalAlignment="Stretch" VerticalAlignment="Top" Background="{StaticResource Colour}">
        <TextBlock Text="Title"/>
    </Grid>

    <Rectangle Fill="{StaticResource Colour}" Margin="0,40,0,0" />
</Grid>

MainWindow.xaml.cs MainWindow.xaml.cs

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 TItleBarBackground
{
    /// <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);
        }
    }
}

Unfortunately, there's a known issue (bug) ongoing.不幸的是,目前存在一个已知问题(错误)。

When they fix the issue, you'll need to override these:当他们解决问题时,您需要覆盖这些:

  • WindowCaptionBackground
  • WindowCaptionBackgroundDisabled

Here's the code.这是代码。

App.xaml申请.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
            <!-- Other merged dictionaries here -->
        </ResourceDictionary.MergedDictionaries>
        <!-- Other app resources here -->
        <Color x:Key="AppCommonBackground">#FF2B2B2B</Color>
        <SolidColorBrush x:Key="WindowCaptionBackground" Color="{StaticResource AppCommonBackground}"/>
        <SolidColorBrush x:Key="WindowCaptionBackgroundDisabled" Color="{StaticResource AppCommonBackground}"/>
    </ResourceDictionary>
</Application.Resources>

MainWindow.xaml主窗口.xaml

<Window
    x:Class="TitleBars.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:TitleBars"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid RowDefinitions="Auto,*" Background="{StaticResource AppCommonBackground}">
        <Grid
            x:Name="AppTitleBar"
            Grid.Row="0"
            Height="28"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Top"
            Canvas.ZIndex="1">
            <TextBlock Text="Title" />
        </Grid>
        <TextBlock
            Grid.Row="1"
            Text="Body" />
    </Grid>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM