DirectoryCreator/ThemeManager.cs

40 lines
1.3 KiB
C#
Raw Normal View History

using System.Windows;
namespace DirectoryCreator
{
public static class ThemeManager
{
2025-02-12 08:27:02 -03:00
private static readonly Uri DarkThemeUri = new Uri("/Themes/DarkTheme.xaml", UriKind.Relative);
private static readonly Uri LightThemeUri = new Uri("/Themes/LightTheme.xaml", UriKind.Relative);
public static void SetTheme(bool isDarkTheme)
{
var app = Application.Current;
2025-02-12 08:27:02 -03:00
if (app == null) return;
2025-02-12 08:27:02 -03:00
var mergedDicts = app.Resources.MergedDictionaries;
2025-02-12 08:27:02 -03:00
// Buscar y eliminar los temas existentes
ResourceDictionary themeToRemove = null;
foreach (ResourceDictionary dict in mergedDicts)
{
2025-02-12 08:27:02 -03:00
if (dict.Source != null && (dict.Source.OriginalString.Contains("LightTheme") || dict.Source.OriginalString.Contains("DarkTheme")))
{
2025-02-12 08:27:02 -03:00
themeToRemove = dict;
break;
}
}
2025-02-12 08:27:02 -03:00
if (themeToRemove != null)
{
2025-02-12 08:27:02 -03:00
mergedDicts.Remove(themeToRemove);
}
2025-02-12 08:27:02 -03:00
// Agregar el nuevo tema
mergedDicts.Add(new ResourceDictionary
{
Source = isDarkTheme ? DarkThemeUri : LightThemeUri
});
}
}
}