using System;
using Avalonia.Reactive;
namespace Avalonia.Controls
{
public static class ResourceNodeExtensions
{
///
/// Finds the specified resource by searching up the logical tree and then global styles.
///
/// The control.
/// The resource key.
/// The resource, or if not found.
public static object FindResource(this IResourceNode control, object key)
{
if (control.TryFindResource(key, out var value))
{
return value;
}
return AvaloniaProperty.UnsetValue;
}
///
/// Tries to the specified resource by searching up the logical tree and then global styles.
///
/// The control.
/// The resource key.
/// On return, contains the resource if found, otherwise null.
/// True if the resource was found; otherwise false.
public static bool TryFindResource(this IResourceNode control, object key, out object value)
{
Contract.Requires(control != null);
Contract.Requires(key != null);
var current = control;
while (current != null)
{
if (current is IResourceNode host)
{
if (host.TryGetResource(key, out value))
{
return true;
}
}
current = current.ResourceParent;
}
value = null;
return false;
}
public static IObservable