Skip to main content
 首页 » 编程设计

c#之更改 Metro 风格应用程序的背景颜色无效

2024年02月20日14cmt

我正在尝试将我在 visual studio (XAML) 中的应用程序的背景颜色更改为白色(或 ApplicationPageBackgroundTheme/或其他名称),但它不起作用。当我调试时,它只显示黑色背景。

当我转到“设备” Pane 并将默认颜色主题选择为“浅色”时,它会使屏幕上的所有内容变白,甚至包括文本和背景。

当我在 xaml 中更改颜色背景时,在运行时它会变回黑色!

我搜索过,但没有找到任何信息。这是一个已知的错误?这以前从未发生过。我正在使用 Visual Studio 2012 Ultimate。

<Page 
    x:Class="hjgjhgjg.MainPage" 
    IsTabStop="false" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:hjgjhgjh" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" Background="White"> 
 
    <Grid Style="{StaticResource LayoutRootStyle}" Background="White"> 
        <Grid.RowDefinitions> 
            <RowDefinition Height="140" /> 
            <RowDefinition Height="*" /> 
        </Grid.RowDefinitions> 
        <Grid> 
            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="Auto" /> 
                <ColumnDefinition Width="*" /> 
            </Grid.ColumnDefinitions> 
            <Button x:Name="backButton" Click="GoBack" IsEnabled="False" Style="{StaticResource BackButtonStyle}" /> 
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="gfdgfdg" Style="{StaticResource PageHeaderTextStyle}" /> 
        </Grid> 
    </Grid> 
</Page> 

请您参考如下方法:

如果没有更多的应用程序/样式,很难确切地知道发生了什么,但是更改主题的一种简单方法是使用 Application 对象的 RequestedTheme property .

例如,在 App.xaml 中,将 RequestedTheme="Light" 设置为 Application 元素的属性:

<Application 
    x:Class="App1.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:App1" 
    RequestedTheme="Light"> 

这很有用,因为 Light 主题会影响所有页面并自动更改文本/按钮/等。如果您只将背景色设为白色,则将其设为黑色而不是一切都是白色。

本演练介绍了这一点以及如何使用您自己的自定义样式覆盖默认样式:

顺便说一句,“设备” Pane 不会更改应用程序设置,但会在设计 View 中模拟应用程序在各种设备功能/设置(屏幕、主题等)下的显示方式。因此,虽然它在设计器中看起来像是一个主题,它将恢复为系统/XAML/代码在您实际运行时产生的任何结果。