如何在不需要地铁口音主题的情况下显示对话框?

How to show dialog box without needing a metro accent theme?

我想显示一个类似 metro 风格的对话框:

public async void Button_Click(object sender, RoutedEventArgs e)
{
   var metroWindow = (Application.Current.MainWindow as MetroWindow);
   await metroWindow.ShowMessageAsync("Title", "Body");
}

然而,它一直抛出错误:

An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll

使用堆栈跟踪:

at MahApps.Metro.Controls.Dialogs.BaseMetroDialog.HandleTheme()

at MahApps.Metro.Controls.Dialogs.BaseMetroDialog.Initialize()

at MahApps.Metro.Controls.Dialogs.BaseMetroDialog..ctor(MetroWindow owningWindow, MetroDialogSettings settings)

...

at 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" />

这足以破坏功能。我可以看到那个主题仍然有效,但是当我尝试触发对话框时,window 会消失但是对话框没有出现,我需要重新启动程序。怎么回事?

MessageDialog md=new MessageDialog("my message");
await md.showAsync();

如果您想创建自定义消息对话框,则必须创建自己的自定义控件。您可以在需要时轻松地向自定义对话框添加样式。

你应该看看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>