I want to show a metro-style dialog box like so:
public async void Button_Click(object sender, RoutedEventArgs e)
{
var metroWindow = (Application.Current.MainWindow as MetroWindow);
await metroWindow.ShowMessageAsync("Title", "Body");
}
However, it kept throwing the error:
An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll
With the stack trace:
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()
So after a lot of fiddling, I realized I needed to include an accent in my App.xaml
resources to give it a color scheme.
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />
However, I've built my program without that and including it has messed with all my styling. Furthermore, there aren't many accents total see here and I can't find one that fits with my scheme.
I've been trying to give the ShowMessageAsync
method it's own color scheme directly with commands such as
metroWindow.MetroDialogOptions.ColorScheme = MetroDialogOptions.ColorScheme
// MetroDialogColorScheme.Theme
// MetroDialogColorScheme.Accented
// MetroDialogColorScheme.Inverted;
But the same error continues to appear. Is there some way to get around this? How could I get the dialog box to use my own style?
Edit
I've actually downloaded the file here: https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Styles/Accents/Cobalt.xaml
And in ResourceDirectory.MergedDictionaries
replaced
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Cobalt.xaml" />
With
<ResourceDictionary Source="Cobalt.xaml" />
And that's enough to break the function. I can see that that theme is still in effect, but when I try to trigger the dialog box, the window will fade but the dialog box doesn't appear and I need to restart the program. What's going on?
You should look at the ThemeManager
. Set the AppStyle
when your application start like so :
ThemeManager.ChangeAppStyle(this, ThemeManager.Accents.First(x => x.Name == "Red"), ThemeManager.DetectAppStyle().Item1);
Here is an example of theme avaible :
To use your own style you can add an accents like so :
ThemeManager.AddAccent("XpertdocBlue", new Uri("XpertdocBlue.xaml", UriKind.Relative));
Try to add a ResourceDictionary
Here is mine:
<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>
And here is my Custom Accent (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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With