我是mahapps.metro主题的新人。当我使用ThemeManager时它只工作一次然后在第二次使用时它会引发一个空引用异常。例如,首先我选择它可以工作的蓝色主题,但在此之后我选择绿色主题它不起作用,并提出一个空参考例外。 这是一个示例代码:
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Blue"),
ThemeManager.GetAppTheme("BaseDark"));
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Green"),
ThemeManager.GetAppTheme("BaseDark"));
出了什么问题?
答案 0 :(得分:0)
这一行应该在App.xaml代码的后面或在你的主Window构造函数中工作。
using System.Windows;
using MahApps.Metro;
namespace MetroDemo
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Blue"),
ThemeManager.GetAppTheme("BaseDark"));
ThemeManager.ChangeAppStyle(Application.Current,
ThemeManager.GetAccent("Green"),
ThemeManager.GetAppTheme("BaseDark"));
}
}
}
确保您要向App.xaml添加必要的资源,因为您想要更改应用程序的资源!
<Application x:Class="MetroDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<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" />
<!-- Accent and AppTheme setting -->
<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>
</Application.Resources>
</Application>
希望这有帮助。