整理控件库
This commit is contained in:
@@ -1,26 +1,30 @@
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
|
||||
using WPFluent.Extensions;
|
||||
|
||||
namespace WPFluent.Appearance;
|
||||
|
||||
/// <summary>
|
||||
/// Allows managing application dictionaries.
|
||||
/// 管理资源字典
|
||||
/// </summary>
|
||||
internal class ResourceDictionaryManager(string searchNamespace)
|
||||
internal class ResourceDictionaryManager
|
||||
{
|
||||
private Collection<ResourceDictionary> GetApplicationMergedDictionaries()
|
||||
{ return UiApplication.Current.Resources.MergedDictionaries; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="ResourceDictionary"/> if exists.
|
||||
/// 获取命名空间,例如正在搜索的资源库。
|
||||
/// </summary>
|
||||
/// <param name="resourceLookup">Any part of the resource name.</param>
|
||||
/// <returns><see cref="ResourceDictionary"/>, <see langword="null"/> if it doesn't exist.</returns>
|
||||
public ResourceDictionary? GetDictionary(string resourceLookup)
|
||||
private string SearchNamespace { get; } = Assembly.GetExecutingAssembly().GetName().Name;
|
||||
|
||||
/// <summary>
|
||||
/// 查找资源 <see cref="ResourceDictionary"/>
|
||||
/// </summary>
|
||||
/// <param name="resourceLookup">任何部分的资源名称。</param>
|
||||
/// <returns><see cref="ResourceDictionary"/>, <see langword="null"/>如果不存在,返回null</returns>
|
||||
public ResourceDictionary? LookupDictionary(string resourceLookup)
|
||||
{
|
||||
Collection<ResourceDictionary> applicationDictionaries = GetApplicationMergedDictionaries();
|
||||
Collection<ResourceDictionary> applicationDictionaries = ThemeManager.Current.Resources.MergedDictionaries;
|
||||
|
||||
if (applicationDictionaries.Count == 0)
|
||||
{
|
||||
@@ -65,47 +69,47 @@ internal class ResourceDictionaryManager(string searchNamespace)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows whether the application contains the <see cref="ResourceDictionary"/>.
|
||||
/// 更新应用程序中的资源字典
|
||||
/// </summary>
|
||||
/// <param name="resourceLookup">Any part of the resource name.</param>
|
||||
/// <returns><see langword="false"/> if it doesn't exist.</returns>
|
||||
public bool HasDictionary(string resourceLookup) { return GetDictionary(resourceLookup) != null; }
|
||||
|
||||
/// <summary>
|
||||
/// 显示应用程序是否包含 <see cref="ResourceDictionary"/>.
|
||||
/// </summary>
|
||||
/// <param name="resourceLookup">资源名称的任何部分。</param>
|
||||
/// <param name="newResourceUri">A valid <see cref="Uri"/> for the replaced resource.</param>
|
||||
/// <returns><see langword="true"/> if the dictionary <see cref="Uri"/> was updated. <see langword="false"/> otherwise.</returns>
|
||||
/// <param name="resourceLookup">要查找的资源名称的一部分</param>
|
||||
/// <param name="newResourceUri">用于替换的新资源的有效Uri</param>
|
||||
/// <returns>如果字典Uri已更新则返回true,否则返回false</returns>
|
||||
public bool UpdateDictionary(string resourceLookup, Uri? newResourceUri)
|
||||
{
|
||||
Collection<ResourceDictionary> applicationDictionaries = UiApplication
|
||||
// 获取应用程序的合并资源字典集合
|
||||
var applicationDictionaries = ThemeManager
|
||||
.Current
|
||||
.Resources
|
||||
.MergedDictionaries;
|
||||
|
||||
// 如果没有资源字典或新的Uri为空,则返回false
|
||||
if (applicationDictionaries.Count == 0 || newResourceUri is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 遍历顶层资源字典
|
||||
for (var i = 0; i < applicationDictionaries.Count; i++)
|
||||
{
|
||||
string sourceUri;
|
||||
|
||||
// 检查当前资源字典是否有Source
|
||||
if (applicationDictionaries[i]?.Source != null)
|
||||
{
|
||||
sourceUri = applicationDictionaries[i].Source.ToString();
|
||||
|
||||
// 检查资源Uri是否匹配搜索条件
|
||||
if (sourceUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase) &&
|
||||
sourceUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// 使用新的Uri更新资源字典
|
||||
applicationDictionaries[i] = new() { Source = newResourceUri };
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历合并的子资源字典
|
||||
for (var j = 0; j < applicationDictionaries[i].MergedDictionaries.Count; j++)
|
||||
{
|
||||
if (applicationDictionaries[i].MergedDictionaries[j]?.Source == null)
|
||||
@@ -115,23 +119,21 @@ internal class ResourceDictionaryManager(string searchNamespace)
|
||||
|
||||
sourceUri = applicationDictionaries[i].MergedDictionaries[j].Source.ToString();
|
||||
|
||||
// 检查子资源字典Uri是否匹配搜索条件
|
||||
if (!sourceUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase) ||
|
||||
!sourceUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
applicationDictionaries[i].MergedDictionaries[j] = new() { Source = newResourceUri };
|
||||
// 使用新的Uri更新子资源字典
|
||||
applicationDictionaries[i].MergedDictionaries[j] = new ResourceDictionary { Source = newResourceUri };
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 未找到匹配的资源字典,返回false
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取命名空间,例如正在搜索的资源库。
|
||||
/// </summary>
|
||||
public string SearchNamespace { get; } = searchNamespace;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user