using System;
using System.Linq;
using System.Reflection;
using System.Windows;
using HandyControl.Data;
using HandyControl.Themes;
namespace HandyControl.Tools
{
///
/// 资源帮助类
///
public class ResourceHelper
{
///
/// 获取资源
///
///
///
public static T GetResource(string key)
{
if (Application.Current.TryFindResource(key) is T resource)
{
return resource;
}
return default;
}
internal static T GetResourceInternal(string key)
{
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().FirstOrDefault(item => Equals(item.Name, name));
}
///
/// 获取皮肤
///
public static ResourceDictionary GetSkin(Assembly assembly, string themePath, SkinType skin)
{
try
{
var uri = new Uri($"pack://application:,,,/{assembly.GetName().Name};component/{themePath}/Skin{skin}.xaml");
return new ResourceDictionary
{
Source = uri
};
}
catch
{
return new ResourceDictionary
{
Source = new Uri($"pack://application:,,,/{assembly.GetName().Name};component/{themePath}/Skin{SkinType.Default}.xaml")
};
}
}
///
/// get HandyControl skin
///
///
///
public static ResourceDictionary GetSkin(SkinType skin) => new()
{
Source = new Uri($"pack://application:,,,/HandyControl;component/Themes/Skin{skin}.xaml")
};
private static ResourceDictionary HcTheme;
///
/// get HandyControl theme
///
///
public static ResourceDictionary GetTheme(bool standalone = false)
{
if (!standalone)
{
HcTheme ??= new ResourceDictionary
{
Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
};
return HcTheme;
}
return new ResourceDictionary
{
Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
};
}
}
}