Browse Source

Merge branch 'master' into optimizeMatchCharacter

pull/10857/head
Max Katz 4 years ago
committed by GitHub
parent
commit
f8180f056a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      src/Avalonia.Base/Controls/ResourceDictionary.cs
  2. 54
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ResourceDictionaryTests.cs

26
src/Avalonia.Base/Controls/ResourceDictionary.cs

@ -15,6 +15,7 @@ namespace Avalonia.Controls
/// </summary>
public class ResourceDictionary : IResourceDictionary
{
private object? lastDeferredItemKey;
private Dictionary<object, object?>? _inner;
private IResourceHost? _owner;
private AvaloniaList<IResourceProvider>? _mergedDictionaries;
@ -241,12 +242,27 @@ namespace Avalonia.Controls
{
if (value is DeferredItem deffered)
{
_inner[key] = value = deffered.Factory(null) switch
// Avoid simple reentrancy, which could commonly occur on redefining the resource.
if (lastDeferredItemKey == key)
{
ITemplateResult t => t.Result,
object v => v,
_ => null,
};
value = null;
return false;
}
try
{
lastDeferredItemKey = key;
_inner[key] = value = deffered.Factory(null) switch
{
ITemplateResult t => t.Result,
{ } v => v,
_ => null,
};
}
finally
{
lastDeferredItemKey = null;
}
}
return true;
}

54
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ResourceDictionaryTests.cs

@ -307,6 +307,60 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
Assert.True(buttonResources.ContainsDeferredKey("Red"));
}
}
[Fact]
public void Should_Be_Possible_To_Redefine_Referenced_Resource_ControlTheme()
{
using (StyledWindow())
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Window.Resources>
<ControlTheme x:Key='{x:Type Button}' TargetType='Button' />
</Window.Resources>
<UserControl>
<UserControl.Resources>
<ControlTheme x:Key='{x:Type Button}' TargetType='Button' BasedOn='{StaticResource {x:Type Button}}' />
</UserControl.Resources>
</UserControl>
</Window>";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var windowResources = (ResourceDictionary)window.Resources;
var innerResources = (ResourceDictionary)((UserControl)window.Content!).Resources;
var winButtonTheme = Assert.IsType<ControlTheme>(windowResources[typeof(Button)]);
var innerButtonTheme = Assert.IsType<ControlTheme>(innerResources[typeof(Button)]);
Assert.Equal(winButtonTheme, innerButtonTheme.BasedOn);
}
}
[Fact]
public void Should_Be_Possible_To_Redefine_Referenced_Resource()
{
using (StyledWindow())
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Window.Resources>
<Color x:Key='SystemAccentColor'>#aaa</Color>
</Window.Resources>
<UserControl>
<UserControl.Resources>
<StaticResource x:Key='SystemAccentColor' ResourceKey='SystemAccentColor' />
</UserControl.Resources>
</UserControl>
</Window>";
var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
var windowResources = (ResourceDictionary)window.Resources;
var innerResources = (ResourceDictionary)((UserControl)window.Content!).Resources;
var winButtonTheme = Assert.IsType<Color>(windowResources["SystemAccentColor"]);
var innerButtonTheme = Assert.IsType<Color>(innerResources["SystemAccentColor"]);
Assert.Equal(winButtonTheme, innerButtonTheme);
}
}
[Fact]
public void Dynamically_Changing_Referenced_Resources_Works_With_DynamicResource()

Loading…
Cancel
Save