Merge pull request #951 from cuiliang/fixDuplicateResourceDictionary
Avoid duplicated ResourceDictionary
This commit is contained in:
2 files changed
+30
-30
No files matched your search
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using HandyControl.Data;
|
||||
@@ -204,23 +205,17 @@ namespace HandyControl.Themes
|
||||
return _precSkin;
|
||||
}
|
||||
|
||||
private void UpdateAccentColor(Color color)
|
||||
public static Theme GetTheme(string name, ResourceDictionary resourceDictionary)
|
||||
{
|
||||
_precSkin[ResourceToken.PrimaryColor] = color;
|
||||
_precSkin[ResourceToken.DarkPrimaryColor] = color;
|
||||
_precSkin[ResourceToken.TitleColor] = color;
|
||||
_precSkin[ResourceToken.SecondaryTitleColor] = color;
|
||||
if (string.IsNullOrEmpty(name) || resourceDictionary == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return resourceDictionary.MergedDictionaries.OfType<Theme>().FirstOrDefault(item => Equals(item.Name, name));
|
||||
}
|
||||
|
||||
private void UpdateSkin() => MergedDictionaries[0] = GetSkin(Skin);
|
||||
|
||||
private ResourceDictionary _theme;
|
||||
|
||||
public virtual ResourceDictionary GetTheme()
|
||||
{
|
||||
_theme ??= ResourceHelper.GetTheme();
|
||||
return _theme;
|
||||
}
|
||||
public virtual ResourceDictionary GetTheme() => ResourceHelper.GetTheme();
|
||||
|
||||
private void InitResource()
|
||||
{
|
||||
@@ -233,10 +228,20 @@ namespace HandyControl.Themes
|
||||
MergedDictionaries.Add(GetSkin(Skin));
|
||||
MergedDictionaries.Add(GetTheme());
|
||||
}
|
||||
|
||||
private void UpdateAccentColor(Color color)
|
||||
{
|
||||
_precSkin[ResourceToken.PrimaryColor] = color;
|
||||
_precSkin[ResourceToken.DarkPrimaryColor] = color;
|
||||
_precSkin[ResourceToken.TitleColor] = color;
|
||||
_precSkin[ResourceToken.SecondaryTitleColor] = color;
|
||||
}
|
||||
|
||||
private void UpdateSkin() => MergedDictionaries[0] = GetSkin(Skin);
|
||||
}
|
||||
|
||||
public class StandaloneTheme : Theme
|
||||
{
|
||||
public override ResourceDictionary GetTheme() => ResourceHelper.GetTheme();
|
||||
public override ResourceDictionary GetTheme() => ResourceHelper.GetStandaloneTheme();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using HandyControl.Data;
|
||||
using HandyControl.Themes;
|
||||
|
||||
namespace HandyControl.Tools
|
||||
{
|
||||
@@ -12,6 +10,8 @@ namespace HandyControl.Tools
|
||||
/// </summary>
|
||||
public class ResourceHelper
|
||||
{
|
||||
private static ResourceDictionary _theme;
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源
|
||||
/// </summary>
|
||||
@@ -37,16 +37,6 @@ namespace HandyControl.Tools
|
||||
return default;
|
||||
}
|
||||
|
||||
public static Theme GetTheme(string name, ResourceDictionary resourceDictionary)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name) || resourceDictionary == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return resourceDictionary.MergedDictionaries.OfType<Theme>().FirstOrDefault(item => Equals(item.Name, name));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取皮肤
|
||||
/// </summary>
|
||||
@@ -82,9 +72,14 @@ namespace HandyControl.Tools
|
||||
/// <summary>
|
||||
/// get HandyControl theme
|
||||
/// </summary>
|
||||
public static ResourceDictionary GetTheme() => new()
|
||||
public static ResourceDictionary GetTheme() => _theme ??= GetStandaloneTheme();
|
||||
|
||||
public static ResourceDictionary GetStandaloneTheme()
|
||||
{
|
||||
Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
|
||||
};
|
||||
return new()
|
||||
{
|
||||
Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user