|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using Avalonia.Controls; |
|
|
|
using Avalonia.Controls.Presenters; |
|
|
|
@ -792,6 +793,82 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions |
|
|
|
Assert.Equal(0xff506070, brush.Color.ToUint32()); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Resource_In_Non_Matching_Style_Is_Not_Resolved() |
|
|
|
{ |
|
|
|
using var app = StyledWindow(); |
|
|
|
|
|
|
|
var xaml = @"
|
|
|
|
<Window xmlns='https://github.com/avaloniaui'
|
|
|
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
|
|
|
|
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions'> |
|
|
|
<Window.Resources> |
|
|
|
<ResourceDictionary> |
|
|
|
<ResourceDictionary.MergedDictionaries> |
|
|
|
<local:TrackingResourceProvider/> |
|
|
|
</ResourceDictionary.MergedDictionaries> |
|
|
|
</ResourceDictionary> |
|
|
|
</Window.Resources> |
|
|
|
|
|
|
|
<Window.Styles> |
|
|
|
<Style Selector='Border.nomatch'> |
|
|
|
<Setter Property='Tag' Value='{DynamicResource foo}'/> |
|
|
|
</Style> |
|
|
|
<Style Selector='Border'> |
|
|
|
<Setter Property='Tag' Value='{DynamicResource bar}'/> |
|
|
|
</Style> |
|
|
|
</Window.Styles> |
|
|
|
|
|
|
|
<Border Name='border'/> |
|
|
|
</Window>";
|
|
|
|
|
|
|
|
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); |
|
|
|
var border = window.FindControl<Border>("border"); |
|
|
|
|
|
|
|
Assert.Equal("bar", border.Tag); |
|
|
|
|
|
|
|
var resourceProvider = (TrackingResourceProvider)window.Resources.MergedDictionaries[0]; |
|
|
|
Assert.Equal(new[] { "bar" }, resourceProvider.RequestedResources); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Resource_In_Non_Active_Style_Is_Not_Resolved() |
|
|
|
{ |
|
|
|
using var app = StyledWindow(); |
|
|
|
|
|
|
|
var xaml = @"
|
|
|
|
<Window xmlns='https://github.com/avaloniaui'
|
|
|
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
|
|
|
|
xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.MarkupExtensions'> |
|
|
|
<Window.Resources> |
|
|
|
<ResourceDictionary> |
|
|
|
<ResourceDictionary.MergedDictionaries> |
|
|
|
<local:TrackingResourceProvider/> |
|
|
|
</ResourceDictionary.MergedDictionaries> |
|
|
|
</ResourceDictionary> |
|
|
|
</Window.Resources> |
|
|
|
|
|
|
|
<Window.Styles> |
|
|
|
<Style Selector='Border'> |
|
|
|
<Setter Property='Tag' Value='{DynamicResource foo}'/> |
|
|
|
</Style> |
|
|
|
<Style Selector='Border'> |
|
|
|
<Setter Property='Tag' Value='{DynamicResource bar}'/> |
|
|
|
</Style> |
|
|
|
</Window.Styles> |
|
|
|
|
|
|
|
<Border Name='border'/> |
|
|
|
</Window>";
|
|
|
|
|
|
|
|
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); |
|
|
|
var border = window.FindControl<Border>("border"); |
|
|
|
|
|
|
|
Assert.Equal("bar", border.Tag); |
|
|
|
|
|
|
|
var resourceProvider = (TrackingResourceProvider)window.Resources.MergedDictionaries[0]; |
|
|
|
Assert.Equal(new[] { "bar" }, resourceProvider.RequestedResources); |
|
|
|
} |
|
|
|
|
|
|
|
private IDisposable StyledWindow(params (string, string)[] assets) |
|
|
|
{ |
|
|
|
var services = TestServices.StyledWindow.With( |
|
|
|
@ -822,4 +899,23 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public class TrackingResourceProvider : IResourceProvider |
|
|
|
{ |
|
|
|
public IResourceHost Owner { get; private set; } |
|
|
|
public bool HasResources => true; |
|
|
|
public List<object> RequestedResources { get; } = new List<object>(); |
|
|
|
|
|
|
|
public event EventHandler OwnerChanged; |
|
|
|
|
|
|
|
public void AddOwner(IResourceHost owner) => Owner = owner; |
|
|
|
public void RemoveOwner(IResourceHost owner) => Owner = null; |
|
|
|
|
|
|
|
public bool TryGetResource(object key, out object value) |
|
|
|
{ |
|
|
|
RequestedResources.Add(key); |
|
|
|
value = key; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|