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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Media;
|
using System.Windows.Media;
|
||||||
using HandyControl.Data;
|
using HandyControl.Data;
|
||||||
@@ -204,23 +205,17 @@ namespace HandyControl.Themes
|
|||||||
return _precSkin;
|
return _precSkin;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateAccentColor(Color color)
|
public static Theme GetTheme(string name, ResourceDictionary resourceDictionary)
|
||||||
{
|
{
|
||||||
_precSkin[ResourceToken.PrimaryColor] = color;
|
if (string.IsNullOrEmpty(name) || resourceDictionary == null)
|
||||||
_precSkin[ResourceToken.DarkPrimaryColor] = color;
|
{
|
||||||
_precSkin[ResourceToken.TitleColor] = color;
|
return null;
|
||||||
_precSkin[ResourceToken.SecondaryTitleColor] = color;
|
}
|
||||||
|
|
||||||
|
return resourceDictionary.MergedDictionaries.OfType<Theme>().FirstOrDefault(item => Equals(item.Name, name));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateSkin() => MergedDictionaries[0] = GetSkin(Skin);
|
public virtual ResourceDictionary GetTheme() => ResourceHelper.GetTheme();
|
||||||
|
|
||||||
private ResourceDictionary _theme;
|
|
||||||
|
|
||||||
public virtual ResourceDictionary GetTheme()
|
|
||||||
{
|
|
||||||
_theme ??= ResourceHelper.GetTheme();
|
|
||||||
return _theme;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitResource()
|
private void InitResource()
|
||||||
{
|
{
|
||||||
@@ -233,10 +228,20 @@ namespace HandyControl.Themes
|
|||||||
MergedDictionaries.Add(GetSkin(Skin));
|
MergedDictionaries.Add(GetSkin(Skin));
|
||||||
MergedDictionaries.Add(GetTheme());
|
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 class StandaloneTheme : Theme
|
||||||
{
|
{
|
||||||
public override ResourceDictionary GetTheme() => ResourceHelper.GetTheme();
|
public override ResourceDictionary GetTheme() => ResourceHelper.GetStandaloneTheme();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using HandyControl.Data;
|
using HandyControl.Data;
|
||||||
using HandyControl.Themes;
|
|
||||||
|
|
||||||
namespace HandyControl.Tools
|
namespace HandyControl.Tools
|
||||||
{
|
{
|
||||||
@@ -12,6 +10,8 @@ namespace HandyControl.Tools
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ResourceHelper
|
public class ResourceHelper
|
||||||
{
|
{
|
||||||
|
private static ResourceDictionary _theme;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取资源
|
/// 获取资源
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -37,16 +37,6 @@ namespace HandyControl.Tools
|
|||||||
return default;
|
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>
|
||||||
/// 获取皮肤
|
/// 获取皮肤
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -82,9 +72,14 @@ namespace HandyControl.Tools
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// get HandyControl theme
|
/// get HandyControl theme
|
||||||
/// </summary>
|
/// </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