WPF window 在 VS 设计器中的颜色与调试时的颜色不同?
WPF window has different colors in VS designer than when debugging?
WPF window 背景颜色在 Visual Studio 设计时是白色的,但是当我调试应用程序时,它是黑色的。为什么?
这是我的 .xaml 代码:
<Controls:MetroWindow x:Class="XLTT.Views.About"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
ShowTitleBar="False"
WindowStartupLocation="CenterOwner"
ShowCloseButton="False"
ResizeMode="NoResize"
WindowStyle="ToolWindow"
Height="320"
Width="400"
Title="About" >
<!-- your content here -->
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Name="lblAppName" Margin="10" TextWrapping="Wrap" Text="Application Name :" VerticalAlignment="Stretch" FontSize="24" FontFamily="Segoe UI Light" Foreground="#FFF53800"/>
<TextBlock Name="lblBuild" Margin="10" TextWrapping="Wrap" Text="Build :" VerticalAlignment="Top" Grid.Row="1" FontSize="16" FontFamily="Segoe UI Light"/>
<TextBlock Name="lblOwner" Margin="10" TextWrapping="Wrap" Text="Owner :" VerticalAlignment="Top" Grid.Row="2" FontWeight="Bold" FontFamily="Segoe UI Light" FontSize="16" />
<TextBlock Name="lblLicense" Margin="10" TextWrapping="Wrap" Text="License" VerticalAlignment="Top" Grid.Row="3" FontWeight="Bold" Foreground="#FFEA1818" FontFamily="Segoe UI Light" FontSize="16" />
<Button Name="btnOk" Content="OK" HorizontalAlignment="Right" Margin="0,0,10,4" VerticalAlignment="Bottom" Width="94" Grid.Row="4" Click="btnOk_Click" Height="40"/>
</Grid>
查看您的 App.xaml
文件。
如果包含:
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
替换为:
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
说明
您的 App.xaml
是程序的入口点,会将样式设置应用于每个子 Window / 页面 / 控件,但 VS 设计器通常无法加载设计中的所有资源模式。所以在 VS 中它将默认为白色,并且在 运行 上应用程序完全应用样式。
MahApps 使用 2 个默认值 style templates 用于 windows:
"BaseLight", "BaseDark"
测试
当然,如果它不在 App.xaml
中,您可以在 MetroWindow 构造函数调试中使用 ThemeManager.DetectAppStyle(this);
检查它。
或者在您的 App.xaml.cs
中覆盖 OnStartup()
方法,如下所示:
public partial class App : Application
{
protected override void OnStartup (StartupEventArgs e)
{
// get the theme from the current application
var theme = ThemeManager.DetectAppStyle(Application.Current);
// now set the Green accent and dark theme
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Blue"),
ThemeManager.GetAppTheme("BaseLight"));
base.OnStartup(e);
}
}
更新
在您告诉我们您的应用程序中没有 App.xaml
入口点之后,问题就很清楚了。 MahApps 需要一些资源字典,您可以在 Quick Start Guide.
中阅读
我认为在您的情况下 您只是缺少 MahApps 的样式。因此,在您的 <Controls:MetroWindow>
标签后添加以下内容:
<Controls:MetroWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Controls:MetroWindow.Resources>
WPF window 背景颜色在 Visual Studio 设计时是白色的,但是当我调试应用程序时,它是黑色的。为什么?
这是我的 .xaml 代码:
<Controls:MetroWindow x:Class="XLTT.Views.About"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
ShowTitleBar="False"
WindowStartupLocation="CenterOwner"
ShowCloseButton="False"
ResizeMode="NoResize"
WindowStyle="ToolWindow"
Height="320"
Width="400"
Title="About" >
<!-- your content here -->
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Name="lblAppName" Margin="10" TextWrapping="Wrap" Text="Application Name :" VerticalAlignment="Stretch" FontSize="24" FontFamily="Segoe UI Light" Foreground="#FFF53800"/>
<TextBlock Name="lblBuild" Margin="10" TextWrapping="Wrap" Text="Build :" VerticalAlignment="Top" Grid.Row="1" FontSize="16" FontFamily="Segoe UI Light"/>
<TextBlock Name="lblOwner" Margin="10" TextWrapping="Wrap" Text="Owner :" VerticalAlignment="Top" Grid.Row="2" FontWeight="Bold" FontFamily="Segoe UI Light" FontSize="16" />
<TextBlock Name="lblLicense" Margin="10" TextWrapping="Wrap" Text="License" VerticalAlignment="Top" Grid.Row="3" FontWeight="Bold" Foreground="#FFEA1818" FontFamily="Segoe UI Light" FontSize="16" />
<Button Name="btnOk" Content="OK" HorizontalAlignment="Right" Margin="0,0,10,4" VerticalAlignment="Bottom" Width="94" Grid.Row="4" Click="btnOk_Click" Height="40"/>
</Grid>
查看您的 App.xaml
文件。
如果包含:
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
替换为:
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
说明
您的 App.xaml
是程序的入口点,会将样式设置应用于每个子 Window / 页面 / 控件,但 VS 设计器通常无法加载设计中的所有资源模式。所以在 VS 中它将默认为白色,并且在 运行 上应用程序完全应用样式。
MahApps 使用 2 个默认值 style templates 用于 windows:
"BaseLight", "BaseDark"
测试
当然,如果它不在 App.xaml
中,您可以在 MetroWindow 构造函数调试中使用 ThemeManager.DetectAppStyle(this);
检查它。
或者在您的 App.xaml.cs
中覆盖 OnStartup()
方法,如下所示:
public partial class App : Application
{
protected override void OnStartup (StartupEventArgs e)
{
// get the theme from the current application
var theme = ThemeManager.DetectAppStyle(Application.Current);
// now set the Green accent and dark theme
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Blue"),
ThemeManager.GetAppTheme("BaseLight"));
base.OnStartup(e);
}
}
更新
在您告诉我们您的应用程序中没有 App.xaml
入口点之后,问题就很清楚了。 MahApps 需要一些资源字典,您可以在 Quick Start Guide.
我认为在您的情况下 您只是缺少 MahApps 的样式。因此,在您的 <Controls:MetroWindow>
标签后添加以下内容:
<Controls:MetroWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Controls:MetroWindow.Resources>