Files
Lattice/Lattice.UI.DragDrop.WinUI/Helpers/ResourceHelper.cs
2026-01-18 16:33:35 +03:00

102 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Windows.UI;
namespace Lattice.UI.DragDrop.WinUI.Helpers;
/// <summary>
/// Вспомогательный класс для работы с ресурсами.
/// </summary>
public static class ResourceHelper
{
/// <summary>
/// Инициализирует ресурсы перетаскивания.
/// </summary>
public static void InitializeDragDropResources()
{
// Загружаем Generic.xaml, если он еще не загружен
var resourceDictionary = new ResourceDictionary();
// В реальном приложении нужно загружать из правильного пути
try
{
resourceDictionary.Source = new System.Uri("ms-appx:///Lattice.UI.DragDrop.WinUI/Themes/Generic.xaml");
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}
catch
{
// Если не удалось загрузить из файла, создаем базовые ресурсы
CreateFallbackResources();
}
}
private static void CreateFallbackResources()
{
var resources = Application.Current.Resources;
// Базовые кисти для визуальной обратной связи
if (!resources.ContainsKey("DragOverBackgroundBrush"))
{
resources["DragOverBackgroundBrush"] = new SolidColorBrush(
Color.FromArgb(76, 0, 120, 215)); // 30% opacity
}
if (!resources.ContainsKey("DragOverBorderBrush"))
{
resources["DragOverBorderBrush"] = new SolidColorBrush(
Color.FromArgb(255, 0, 120, 215));
}
if (!resources.ContainsKey("DropValidBrush"))
{
resources["DropValidBrush"] = new SolidColorBrush(
Color.FromArgb(255, 0, 204, 0));
}
if (!resources.ContainsKey("DropInvalidBrush"))
{
resources["DropInvalidBrush"] = new SolidColorBrush(
Color.FromArgb(255, 255, 0, 0));
}
}
/// <summary>
/// Получает стиль из ресурсов.
/// </summary>
public static Style? GetStyle(string styleKey)
{
if (Application.Current.Resources.TryGetValue(styleKey, out var style) && style is Style)
{
return style as Style;
}
return null;
}
/// <summary>
/// Получает кисть из ресурсов.
/// </summary>
public static Brush? GetBrush(string brushKey)
{
if (Application.Current.Resources.TryGetValue(brushKey, out var brush) && brush is Brush)
{
return brush as Brush;
}
return null;
}
/// <summary>
/// Добавляет или обновляет ресурс.
/// </summary>
public static void SetResource(string key, object value)
{
Application.Current.Resources[key] = value;
}
/// <summary>
/// Проверяет существование ресурса.
/// </summary>
public static bool HasResource(string key)
{
return Application.Current.Resources.ContainsKey(key);
}
}