我想像这样显示一个都市风格的对话框:
public async void Button_Click(object sender, RoutedEventArgs e)
{
var metroWindow = (Application.Current.MainWindow as MetroWindow);
await metroWindow.ShowMessageAsync("Title", "Body");
}
然而,它一直在抛出错误:
mscorlib.dll中出现未处理的“System.NullReferenceException”类型异常
使用堆栈跟踪:
在MahApps.Metro.Controls.Dialogs.BaseMetroDialog.HandleTheme()
at MahApps.Metro.Controls.Dialogs.BaseMetroDialog.Initialize()
在MahApps.Metro.Controls.Dialogs.BaseMetroDialog..ctor(MetroWindow owningWindow,MetroDialogSettings设置)
...
在System.Threading.ThreadHelper.ThreadStart()
所以经过大量的摆弄后,我意识到我需要在我的App.xaml
资源中添加一个重音来为它提供配色方案。
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />
但是,我已经构建了没有它的程序,包括它已经搞砸了我的所有样式。此外,总共see here的口音不多,我找不到符合我方案的口音。
我一直试图直接使用
等命令为ShowMessageAsync
方法提供自己的配色方案
metroWindow.MetroDialogOptions.ColorScheme = MetroDialogOptions.ColorScheme
// MetroDialogColorScheme.Theme
// MetroDialogColorScheme.Accented
// MetroDialogColorScheme.Inverted;
但同样的错误仍然存在。有办法解决这个问题吗?我怎么能让对话框使用我自己的风格?
修改
我实际上已经在这里下载了文件:https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Styles/Accents/Cobalt.xaml
并在ResourceDirectory.MergedDictionaries
替换
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />
使用
<ResourceDictionary Source="Cobalt.xaml" />
这足以打破这个功能。我可以看到该主题仍然有效,但是当我尝试触发对话框时,窗口将会淡出,但对话框不会出现,我需要重新启动程序。发生了什么事?
答案 0 :(得分:3)
您应该查看ThemeManager
。应用程序启动时设置AppStyle
:
ThemeManager.ChangeAppStyle(this, ThemeManager.Accents.First(x => x.Name == "Red"), ThemeManager.DetectAppStyle().Item1);
以下是可用主题的示例:
要使用您自己的风格,您可以添加如下的重音:
ThemeManager.AddAccent("XpertdocBlue", new Uri("XpertdocBlue.xaml", UriKind.Relative));
尝试添加ResourceDictionary
这是我的:
<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/BaseLight.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Xpertdoc.PortalWordAddIn.Views;component/Resources/Icons.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
<ResourceDictionary Source="XpertdocBlue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</controls:MetroWindow.Resources>
这是我的自定义重音(XpertdocBlue.xaml
):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="HighlightColor">#FF2B579A</Color>
<Color x:Key="AccentColor">#FF2B579A</Color>
<!--60%-->
<Color x:Key="AccentColor2">#CC2B579A</Color>
<!--40%-->
<Color x:Key="AccentColor3">#992B579A</Color>
<!--20%-->
<Color x:Key="AccentColor4">#662B579A</Color>
<!-- re-set brushes too -->
<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" />
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}"/>
<SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}"/>
<SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}"/>
<SolidColorBrush x:Key="AccentColorBrush4" Color="{StaticResource AccentColor4}"/>
<SolidColorBrush x:Key="WindowTitleColorBrush" Color="{StaticResource AccentColor}" />
<SolidColorBrush x:Key="AccentSelectedColorBrush" Color="White" />
<LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5">
<GradientStop Color="{StaticResource HighlightColor}" Offset="0" />
<GradientStop Color="{StaticResource AccentColor3}" Offset="1" />
</LinearGradientBrush>
<SolidColorBrush x:Key="CheckmarkFill" Color="{StaticResource AccentColor}" />
<SolidColorBrush x:Key="RightArrowFill" Color="{StaticResource AccentColor}" />
<Color x:Key="IdealForegroundColor">White</Color>
<SolidColorBrush x:Key="IdealForegroundColorBrush" Color="{StaticResource IdealForegroundColor}"/>
</ResourceDictionary>
答案 1 :(得分:1)
MessageDialog md=new MessageDialog("my message");
await md.showAsync();
如果要创建自定义消息对话框,则必须创建自己的自定义控件。您可以在需要时轻松地将样式添加到自定义对话框中。