Skip to content

Commit 4735ebc

Browse files
authored
Merge pull request #951 from cuiliang/fixDuplicateResourceDictionary
Avoid duplicated ResourceDictionary
2 parents bca2568 + 8960758 commit 4735ebc

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

src/Shared/HandyControl_Shared/Themes/Theme.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Windows;
45
using System.Windows.Media;
56
using HandyControl.Data;
@@ -204,24 +205,18 @@ public virtual ResourceDictionary GetSkin(SkinType skinType)
204205
return _precSkin;
205206
}
206207

207-
private void UpdateAccentColor(Color color)
208+
public static Theme GetTheme(string name, ResourceDictionary resourceDictionary)
208209
{
209-
_precSkin[ResourceToken.PrimaryColor] = color;
210-
_precSkin[ResourceToken.DarkPrimaryColor] = color;
211-
_precSkin[ResourceToken.TitleColor] = color;
212-
_precSkin[ResourceToken.SecondaryTitleColor] = color;
213-
}
214-
215-
private void UpdateSkin() => MergedDictionaries[0] = GetSkin(Skin);
216-
217-
private ResourceDictionary _theme;
210+
if (string.IsNullOrEmpty(name) || resourceDictionary == null)
211+
{
212+
return null;
213+
}
218214

219-
public virtual ResourceDictionary GetTheme()
220-
{
221-
_theme ??= ResourceHelper.GetTheme();
222-
return _theme;
215+
return resourceDictionary.MergedDictionaries.OfType<Theme>().FirstOrDefault(item => Equals(item.Name, name));
223216
}
224217

218+
public virtual ResourceDictionary GetTheme() => ResourceHelper.GetTheme();
219+
225220
private void InitResource()
226221
{
227222
if (DesignerHelper.IsInDesignMode)
@@ -233,10 +228,20 @@ private void InitResource()
233228
MergedDictionaries.Add(GetSkin(Skin));
234229
MergedDictionaries.Add(GetTheme());
235230
}
231+
232+
private void UpdateAccentColor(Color color)
233+
{
234+
_precSkin[ResourceToken.PrimaryColor] = color;
235+
_precSkin[ResourceToken.DarkPrimaryColor] = color;
236+
_precSkin[ResourceToken.TitleColor] = color;
237+
_precSkin[ResourceToken.SecondaryTitleColor] = color;
238+
}
239+
240+
private void UpdateSkin() => MergedDictionaries[0] = GetSkin(Skin);
236241
}
237242

238243
public class StandaloneTheme : Theme
239244
{
240-
public override ResourceDictionary GetTheme() => ResourceHelper.GetTheme();
245+
public override ResourceDictionary GetTheme() => ResourceHelper.GetStandaloneTheme();
241246
}
242247
}

src/Shared/HandyControl_Shared/Tools/Helper/ResourceHelper.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using System.Linq;
32
using System.Reflection;
43
using System.Windows;
54
using HandyControl.Data;
6-
using HandyControl.Themes;
75

86
namespace HandyControl.Tools
97
{
@@ -12,6 +10,8 @@ namespace HandyControl.Tools
1210
/// </summary>
1311
public class ResourceHelper
1412
{
13+
private static ResourceDictionary _theme;
14+
1515
/// <summary>
1616
/// 获取资源
1717
/// </summary>
@@ -37,16 +37,6 @@ internal static T GetResourceInternal<T>(string key)
3737
return default;
3838
}
3939

40-
public static Theme GetTheme(string name, ResourceDictionary resourceDictionary)
41-
{
42-
if (string.IsNullOrEmpty(name) || resourceDictionary == null)
43-
{
44-
return null;
45-
}
46-
47-
return resourceDictionary.MergedDictionaries.OfType<Theme>().FirstOrDefault(item => Equals(item.Name, name));
48-
}
49-
5040
/// <summary>
5141
/// 获取皮肤
5242
/// </summary>
@@ -82,9 +72,14 @@ public static ResourceDictionary GetSkin(Assembly assembly, string themePath, Sk
8272
/// <summary>
8373
/// get HandyControl theme
8474
/// </summary>
85-
public static ResourceDictionary GetTheme() => new()
75+
public static ResourceDictionary GetTheme() => _theme ??= GetStandaloneTheme();
76+
77+
public static ResourceDictionary GetStandaloneTheme()
8678
{
87-
Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
88-
};
79+
return new()
80+
{
81+
Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
82+
};
83+
}
8984
}
9085
}

0 commit comments

Comments
 (0)