now hc styles can be referenced in window.resource

by the way, supported multiple themes at the same time, It may be related to #574
This commit is contained in:
NaBian committed 2021-05-30 01:47:30 +08:00
1 parent 3be68c3010
commit d997aee5f7
23 files changed
+161 -129

No files matched your search

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows;
@@ -13,29 +12,6 @@ namespace HandyControl.Tools
/// </summary>
public class ResourceHelper
{
/// <summary>
/// 获取字符串
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetString(string key) => Application.Current.TryFindResource(key) as string;
/// <summary>
/// 获取字符串
/// </summary>
/// <param name="separator"></param>
/// <param name="keyArr"></param>
/// <returns></returns>
public static string GetString(string separator = ";", params string[] keyArr) =>
string.Join(separator, keyArr.Select(key => Application.Current.TryFindResource(key) as string).ToList());
/// <summary>
/// 获取字符串
/// </summary>
/// <param name="keyArr"></param>
/// <returns></returns>
public static List<string> GetStringList(params string[] keyArr) => keyArr.Select(key => Application.Current.TryFindResource(key) as string).ToList();
/// <summary>
/// 获取资源
/// </summary>
@@ -51,23 +27,29 @@ namespace HandyControl.Tools
return default;
}
/// <summary>
/// 获取HandyControl皮肤
/// </summary>
/// <param name="skin"></param>
/// <returns></returns>
public static ResourceDictionary GetSkin(SkinType skin) => new ResourceDictionary
internal static T GetResourceInternal<T>(string key)
{
Source = new Uri($"pack://application:,,,/HandyControl;component/Themes/Skin{skin.ToString()}.xaml")
};
if (GetTheme()[key] is T resource)
{
return resource;
}
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>
/// <param name="assembly"></param>
/// <param name="themePath"></param>
/// <param name="skin"></param>
/// <returns></returns>
public static ResourceDictionary GetSkin(Assembly assembly, string themePath, SkinType skin)
{
try
@@ -87,11 +69,23 @@ namespace HandyControl.Tools
}
}
public static Theme GetTheme(string name, ResourceDictionary resourceDictionary)
/// <summary>
/// get HandyControl skin
/// </summary>
/// <param name="skin"></param>
/// <returns></returns>
public static ResourceDictionary GetSkin(SkinType skin) => new()
{
if (string.IsNullOrEmpty(name) || resourceDictionary == null) return null;
return resourceDictionary.MergedDictionaries.OfType<Theme>()
.FirstOrDefault(item => Equals(item.Name, name));
}
Source = new Uri($"pack://application:,,,/HandyControl;component/Themes/Skin{skin}.xaml")
};
/// <summary>
/// get HandyControl theme
/// </summary>
/// <returns></returns>
public static ResourceDictionary GetTheme() => new Lazy<ResourceDictionary>(() => new ResourceDictionary
{
Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
}).Value;
}
}