23 changed files with 249 additions and 189 deletions
@ -0,0 +1,22 @@ |
|||
using System; |
|||
|
|||
namespace Avalonia.Controls |
|||
{ |
|||
/// <summary>
|
|||
/// Defines an element that can be queried for resources.
|
|||
/// </summary>
|
|||
public interface IResourceHost |
|||
{ |
|||
/// <summary>
|
|||
/// Tries to find a resource within the element.
|
|||
/// </summary>
|
|||
/// <param name="key">The resource key.</param>
|
|||
/// <param name="value">
|
|||
/// When this method returns, contains the value associated with the specified key,
|
|||
/// if the key is found; otherwise, null
|
|||
/// <returns>
|
|||
/// True if the resource if found, otherwise false.
|
|||
/// </returns>
|
|||
bool TryGetResource(string key, out object value); |
|||
} |
|||
} |
|||
@ -1,39 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
|
|||
namespace Avalonia.Styling |
|||
{ |
|||
public static class StyleExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Tries to find a named style resource.
|
|||
/// </summary>
|
|||
/// <param name="control">The control from which to find the resource.</param>
|
|||
/// <param name="name">The resource name.</param>
|
|||
/// <returns>
|
|||
/// The resource if found, otherwise <see cref="AvaloniaProperty.UnsetValue"/>.
|
|||
/// </returns>
|
|||
public static object FindStyleResource(this IStyleHost control, string name) |
|||
{ |
|||
Contract.Requires<ArgumentNullException>(control != null); |
|||
Contract.Requires<ArgumentNullException>(name != null); |
|||
Contract.Requires<ArgumentException>(!string.IsNullOrWhiteSpace(name)); |
|||
|
|||
while (control != null) |
|||
{ |
|||
var result = control.Styles.FindResource(name); |
|||
|
|||
if (result != AvaloniaProperty.UnsetValue) |
|||
{ |
|||
return result; |
|||
} |
|||
|
|||
control = control.StylingParent; |
|||
} |
|||
|
|||
return AvaloniaProperty.UnsetValue; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,87 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using Avalonia.Styling; |
|||
using Avalonia.UnitTests; |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.Controls.UnitTests |
|||
{ |
|||
public class ControlTests_Resources |
|||
{ |
|||
[Fact] |
|||
public void FindResource_Should_Find_Control_Resource() |
|||
{ |
|||
var target = new Control |
|||
{ |
|||
Resources = |
|||
{ |
|||
{ "foo", "foo-value" }, |
|||
} |
|||
}; |
|||
|
|||
Assert.Equal("foo-value", target.FindResource("foo")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void FindResource_Should_Find_Control_Resource_In_Parent() |
|||
{ |
|||
Control target; |
|||
|
|||
var root = new Decorator |
|||
{ |
|||
Resources = |
|||
{ |
|||
{ "foo", "foo-value" }, |
|||
}, |
|||
Child = target = new Control(), |
|||
}; |
|||
|
|||
Assert.Equal("foo-value", target.FindResource("foo")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void FindResource_Should_Find_Application_Resource() |
|||
{ |
|||
Control target; |
|||
|
|||
var app = new Application |
|||
{ |
|||
Resources = |
|||
{ |
|||
{ "foo", "foo-value" }, |
|||
}, |
|||
}; |
|||
|
|||
var root = new TestRoot |
|||
{ |
|||
Child = target = new Control(), |
|||
StylingParent = app, |
|||
}; |
|||
|
|||
Assert.Equal("foo-value", target.FindResource("foo")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void FindResource_Should_Find_Style_Resource() |
|||
{ |
|||
var target = new Control |
|||
{ |
|||
Styles = |
|||
{ |
|||
new Style |
|||
{ |
|||
Resources = |
|||
{ |
|||
{ "foo", "foo-value" }, |
|||
} |
|||
} |
|||
} |
|||
}; |
|||
|
|||
Assert.Equal("foo-value", target.FindResource("foo")); |
|||
} |
|||
|
|||
} |
|||
} |
|||
@ -1,78 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System.Collections.Generic; |
|||
using Avalonia.Controls; |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.Styling.UnitTests |
|||
{ |
|||
public class ResourceTests |
|||
{ |
|||
[Fact] |
|||
public void FindStyleResource_Should_Find_Correct_Resource() |
|||
{ |
|||
Border target; |
|||
|
|||
var tree = new Decorator |
|||
{ |
|||
Styles = new Styles |
|||
{ |
|||
new Style |
|||
{ |
|||
Resources = |
|||
{ |
|||
{ "Foo", "foo resource" }, |
|||
{ "Bar", "overridden" }, |
|||
} |
|||
} |
|||
}, |
|||
Child = target = new Border |
|||
{ |
|||
Styles = new Styles |
|||
{ |
|||
new Style |
|||
{ |
|||
Resources = |
|||
{ |
|||
{ "Bar", "again overridden" }, |
|||
} |
|||
}, |
|||
new Style |
|||
{ |
|||
Resources = |
|||
{ |
|||
{ "Bar", "bar resource" }, |
|||
} |
|||
} |
|||
} |
|||
} |
|||
}; |
|||
|
|||
Assert.Equal("foo resource", target.FindStyleResource("Foo")); |
|||
Assert.Equal("bar resource", target.FindStyleResource("Bar")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void FindStyleResource_Should_Return_UnsetValue_For_Not_Found() |
|||
{ |
|||
Border target; |
|||
|
|||
var tree = target = new Border |
|||
{ |
|||
Styles = new Styles |
|||
{ |
|||
new Style |
|||
{ |
|||
Resources = |
|||
{ |
|||
{ "Foo", "foo" }, |
|||
} |
|||
}, |
|||
} |
|||
}; |
|||
|
|||
Assert.Equal(AvaloniaProperty.UnsetValue, target.FindStyleResource("Baz")); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue