Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MaterialDesignThemes.Wpf/BundledTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MaterialDesignThemes.Wpf
{
public class BundledTheme : ResourceDictionary
public class BundledTheme : ResourceDictionary, IMaterialDesignThemeDictionary
{
private BaseTheme? _baseTheme;
public BaseTheme? BaseTheme
Expand Down
2 changes: 1 addition & 1 deletion MaterialDesignThemes.Wpf/CustomColorTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MaterialDesignThemes.Wpf
{
public class CustomColorTheme : ResourceDictionary
public class CustomColorTheme : ResourceDictionary, IMaterialDesignThemeDictionary
{
private BaseTheme? _baseTheme;
public BaseTheme? BaseTheme
Expand Down
5 changes: 5 additions & 0 deletions MaterialDesignThemes.Wpf/IMaterialDesignThemeDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace MaterialDesignThemes.Wpf
{
public interface IMaterialDesignThemeDictionary
{ }
}
18 changes: 12 additions & 6 deletions MaterialDesignThemes.Wpf/PaletteHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Windows;

namespace MaterialDesignThemes.Wpf
Expand All @@ -8,23 +9,28 @@ public class PaletteHelper
public virtual ITheme GetTheme()
{
if (Application.Current is null)
throw new InvalidOperationException("Cannot get theme outside of a WPF application. Use ResourceDictionaryExtensions.GetTheme on the appropriate resource dictionary instead.");
return Application.Current.Resources.GetTheme();
throw new InvalidOperationException($"Cannot get theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetTheme)} on the appropriate resource dictionary instead.");
return GetResourceDictionary().GetTheme();
}

public virtual void SetTheme(ITheme theme)
{
if (theme is null) throw new ArgumentNullException(nameof(theme));
if (Application.Current is null)
throw new InvalidOperationException("Cannot set theme outside of a WPF application. Use ResourceDictionaryExtensions.SetTheme on the appropriate resource dictionary instead.");
Application.Current.Resources.SetTheme(theme);
throw new InvalidOperationException($"Cannot set theme outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.SetTheme)} on the appropriate resource dictionary instead.");

GetResourceDictionary().SetTheme(theme);
}

public virtual IThemeManager? GetThemeManager()
{
if (Application.Current is null)
throw new InvalidOperationException("Cannot get ThemeManager. Use ResourceDictionaryExtensions.GetThemeManager on the appropriate resource dictionary instead.");
return Application.Current.Resources.GetThemeManager();
throw new InvalidOperationException($"Cannot get ThemeManager outside of a WPF application. Use {nameof(ResourceDictionaryExtensions)}.{nameof(ResourceDictionaryExtensions.GetThemeManager)} on the appropriate resource dictionary instead.");
return GetResourceDictionary().GetThemeManager();
}

private static ResourceDictionary GetResourceDictionary()
=> Application.Current.Resources.MergedDictionaries.FirstOrDefault(x => x is IMaterialDesignThemeDictionary) ??
Application.Current.Resources;
}
}