2025-02-10 20:53:40 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
using WPFluent.Extensions;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WPFluent.Appearance;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Allows managing application dictionaries.
|
|
|
|
|
|
/// </summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
internal class ResourceDictionaryManager(string searchNamespace)
|
2025-02-10 20:53:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
Collection<ResourceDictionary> applicationDictionaries = GetApplicationMergedDictionaries();
|
|
|
|
|
|
|
|
|
|
|
|
if (applicationDictionaries.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (ResourceDictionary t in applicationDictionaries)
|
|
|
|
|
|
{
|
|
|
|
|
|
string resourceDictionaryUri;
|
|
|
|
|
|
|
|
|
|
|
|
if (t?.Source != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
resourceDictionaryUri = t.Source.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
if (resourceDictionaryUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
|
|
resourceDictionaryUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
return t;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (ResourceDictionary? t1 in t!.MergedDictionaries)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (t1?.Source == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resourceDictionaryUri = t1.Source.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
if (!resourceDictionaryUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
|
!resourceDictionaryUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return t1;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// 显示应用程序是否包含 <see cref="ResourceDictionary"/>.
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// <param name="resourceLookup">资源名称的任何部分。</param>
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// <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>
|
|
|
|
|
|
public bool UpdateDictionary(string resourceLookup, Uri? newResourceUri)
|
|
|
|
|
|
{
|
|
|
|
|
|
Collection<ResourceDictionary> applicationDictionaries = UiApplication
|
|
|
|
|
|
.Current
|
|
|
|
|
|
.Resources
|
|
|
|
|
|
.MergedDictionaries;
|
|
|
|
|
|
|
|
|
|
|
|
if (applicationDictionaries.Count == 0 || newResourceUri is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < applicationDictionaries.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
string sourceUri;
|
|
|
|
|
|
|
|
|
|
|
|
if (applicationDictionaries[i]?.Source != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
sourceUri = applicationDictionaries[i].Source.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
if (sourceUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
|
|
sourceUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
applicationDictionaries[i] = new() { Source = newResourceUri };
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (var j = 0; j < applicationDictionaries[i].MergedDictionaries.Count; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (applicationDictionaries[i].MergedDictionaries[j]?.Source == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sourceUri = applicationDictionaries[i].MergedDictionaries[j].Source.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
if (!sourceUri.Contains(SearchNamespace, StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
|
!sourceUri.Contains(resourceLookup, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
applicationDictionaries[i].MergedDictionaries[j] = new() { Source = newResourceUri };
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
/// 获取命名空间,例如正在搜索的资源库。
|
2025-02-10 20:53:40 +08:00
|
|
|
|
/// </summary>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
public string SearchNamespace { get; } = searchNamespace;
|
2025-02-10 20:53:40 +08:00
|
|
|
|
}
|