3 changed files with 171 additions and 0 deletions
@ -0,0 +1,45 @@ |
|||||
|
// 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 System.ComponentModel; |
||||
|
using Avalonia.Controls; |
||||
|
using Portable.Xaml; |
||||
|
using Portable.Xaml.ComponentModel; |
||||
|
using Portable.Xaml.Markup; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.MarkupExtensions |
||||
|
{ |
||||
|
public class StaticResourceExtension : MarkupExtension |
||||
|
{ |
||||
|
public StaticResourceExtension() |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public StaticResourceExtension(string resourceKey) |
||||
|
{ |
||||
|
ResourceKey = resourceKey; |
||||
|
} |
||||
|
|
||||
|
public string ResourceKey { get; set; } |
||||
|
|
||||
|
public override object ProvideValue(IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
var context = (ITypeDescriptorContext)serviceProvider; |
||||
|
var schemaContext = context.GetService<IXamlSchemaContextProvider>()?.SchemaContext; |
||||
|
var ambientProvider = context.GetService<IAmbientProvider>(); |
||||
|
var resourceProviderType = schemaContext.GetXamlType(typeof(IResourceProvider)); |
||||
|
var resourceProviders = ambientProvider.GetAllAmbientValues(resourceProviderType); |
||||
|
|
||||
|
foreach (IResourceProvider resourceProvider in resourceProviders) |
||||
|
{ |
||||
|
if (resourceProvider.TryGetResource(ResourceKey, out var value)) |
||||
|
{ |
||||
|
return value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return AvaloniaProperty.UnsetValue; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,125 @@ |
|||||
|
// 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.Controls; |
||||
|
using Avalonia.Markup.Xaml.Data; |
||||
|
using Avalonia.Media; |
||||
|
using Avalonia.Styling; |
||||
|
using Avalonia.UnitTests; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.UnitTests.Xaml |
||||
|
{ |
||||
|
public class StaticResourceTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void StaticResource_Style_Can_Be_Assigned_To_Property() |
||||
|
{ |
||||
|
var xaml = @"
|
||||
|
<UserControl xmlns='https://github.com/avaloniaui'
|
||||
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
|
||||
|
<UserControl.Resources> |
||||
|
<SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> |
||||
|
</UserControl.Resources> |
||||
|
|
||||
|
<Border Name='border' Background='{StaticResource brush}'/> |
||||
|
</UserControl>";
|
||||
|
|
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var userControl = (UserControl)loader.Load(xaml); |
||||
|
var border = userControl.FindControl<Border>("border"); |
||||
|
|
||||
|
var brush = (SolidColorBrush)border.Background; |
||||
|
Assert.Equal(0xff506070, brush.Color.ToUint32()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void StaticResource_From_Style_Can_Be_Assigned_To_Property() |
||||
|
{ |
||||
|
var xaml = @"
|
||||
|
<UserControl xmlns='https://github.com/avaloniaui'
|
||||
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
|
||||
|
<UserControl.Styles> |
||||
|
<Style> |
||||
|
<Style.Resources> |
||||
|
<SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> |
||||
|
</Style.Resources> |
||||
|
</Style> |
||||
|
</UserControl.Styles> |
||||
|
|
||||
|
<Border Name='border' Background='{StaticResource brush}'/> |
||||
|
</UserControl>";
|
||||
|
|
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var userControl = (UserControl)loader.Load(xaml); |
||||
|
var border = userControl.FindControl<Border>("border"); |
||||
|
|
||||
|
var brush = (SolidColorBrush)border.Background; |
||||
|
Assert.Equal(0xff506070, brush.Color.ToUint32()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void StaticResource_Can_Be_Assigned_To_Setter() |
||||
|
{ |
||||
|
using (StyledWindowNoTheme()) |
||||
|
{ |
||||
|
var xaml = @"
|
||||
|
<Window xmlns='https://github.com/avaloniaui'
|
||||
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
|
||||
|
<Window.Resources> |
||||
|
<SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> |
||||
|
</Window.Resources> |
||||
|
<Window.Styles> |
||||
|
<Style Selector='Button'> |
||||
|
<Setter Property='Background' Value='{StaticResource brush}'/> |
||||
|
</Style> |
||||
|
</Window.Styles> |
||||
|
<Button Name='button'/> |
||||
|
</Window>";
|
||||
|
|
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var window = (Window)loader.Load(xaml); |
||||
|
var button = window.FindControl<Button>("button"); |
||||
|
var brush = (SolidColorBrush)button.Background; |
||||
|
|
||||
|
Assert.Equal(0xff506070, brush.Color.ToUint32()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void StaticResource_From_Style_Can_Be_Assigned_To_Setter() |
||||
|
{ |
||||
|
using (StyledWindowNoTheme()) |
||||
|
{ |
||||
|
var xaml = @"
|
||||
|
<Window xmlns='https://github.com/avaloniaui'
|
||||
|
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
|
||||
|
<Window.Styles> |
||||
|
<Style> |
||||
|
<Style.Resources> |
||||
|
<SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush> |
||||
|
</Style.Resources> |
||||
|
</Style> |
||||
|
<Style Selector='Button'> |
||||
|
<Setter Property='Background' Value='{StaticResource brush}'/> |
||||
|
</Style> |
||||
|
</Window.Styles> |
||||
|
<Button Name='button'/> |
||||
|
</Window>";
|
||||
|
|
||||
|
var loader = new AvaloniaXamlLoader(); |
||||
|
var window = (Window)loader.Load(xaml); |
||||
|
var button = window.FindControl<Button>("button"); |
||||
|
var brush = (SolidColorBrush)button.Background; |
||||
|
|
||||
|
Assert.Equal(0xff506070, brush.Color.ToUint32()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private IDisposable StyledWindowNoTheme() |
||||
|
{ |
||||
|
return UnitTestApplication.Start(TestServices.StyledWindow.With(theme: () => new Styles())); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue