在Visual Studio的设计过程中,WPF窗口背景颜色为白色,但是当我调试应用程序时,它是黑色的。为什么呢?
这是我的.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>
答案 0 :(得分:2)
查看您的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
是程序的入口点,它将样式设置应用于每个子窗口/页面/控件,但VS Designer通常无法在设计模式下加载所有资源。所以在VS中它将是默认的白色并且在运行应用程序时完全应用样式。
MahApps为Windows使用2个默认style templates:
“BaseLight”,“BaseDark”
<强>测试强>
当然,如果它不在App.xaml
中,您可以在MetroWindow构造函数调试中使用ThemeManager.DetectAppStyle(this);
进行检查。
或覆盖App.xaml.cs
OnStartup()
Methode,如下所示:
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>