Browse Source

Optimizing resource related code.

pull/8064/head
Dariusz Komosinski 4 years ago
parent
commit
bb8aaee1e0
  1. 11
      src/Avalonia.Base/Controls/ResourceNodeExtensions.cs
  2. 2
      src/Avalonia.Base/Styling/IStyle.cs
  3. 2
      src/Avalonia.Base/Styling/Styles.cs
  4. 10
      src/Avalonia.Themes.Default/Controls/DataValidationErrors.xaml
  5. 17
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs
  6. 6
      src/Markup/Avalonia.Markup.Xaml/Styling/StyleInclude.cs

11
src/Avalonia.Base/Controls/ResourceNodeExtensions.cs

@ -40,19 +40,16 @@ namespace Avalonia.Controls
control = control ?? throw new ArgumentNullException(nameof(control));
key = key ?? throw new ArgumentNullException(nameof(key));
IResourceHost? current = control;
IResourceNode? current = control;
while (current != null)
{
if (current is IResourceHost host)
if (current.TryGetResource(key, out value))
{
if (host.TryGetResource(key, out value))
{
return true;
}
return true;
}
current = (current as IStyledElement)?.StylingParent as IResourceHost;
current = (current as IStyledElement)?.StylingParent as IResourceNode;
}
value = null;

2
src/Avalonia.Base/Styling/IStyle.cs

@ -8,7 +8,7 @@ namespace Avalonia.Styling
/// <summary>
/// Defines the interface for styles.
/// </summary>
public interface IStyle
public interface IStyle : IResourceNode
{
/// <summary>
/// Gets a collection of child styles.

2
src/Avalonia.Base/Styling/Styles.cs

@ -160,7 +160,7 @@ namespace Avalonia.Styling
for (var i = Count - 1; i >= 0; --i)
{
if (this[i] is IResourceProvider p && p.TryGetResource(key, out value))
if (this[i].TryGetResource(key, out value))
{
return true;
}

10
src/Avalonia.Themes.Default/Controls/DataValidationErrors.xaml

@ -1,9 +1,13 @@
<Style xmlns="https://github.com/avaloniaui"
Selector="DataValidationErrors">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Selector="DataValidationErrors"
x:CompileBindings="True"
x:DataType="DataValidationErrors">
<Setter Property="Template">
<ControlTemplate>
<DockPanel LastChildFill="True">
<ContentControl DockPanel.Dock="Right"
<ContentControl x:DataType="DataValidationErrors"
DockPanel.Dock="Right"
ContentTemplate="{TemplateBinding ErrorTemplate}"
DataContext="{TemplateBinding Owner}"
Content="{Binding (DataValidationErrors.Errors)}"
@ -30,7 +34,7 @@
</Style>
</Canvas.Styles>
<ToolTip.Tip>
<ItemsControl Items="{Binding}"/>
<ItemsControl x:DataType="DataValidationErrors" Items="{Binding}"/>
</ToolTip.Tip>
<Path Data="M14,7 A7,7 0 0,0 0,7 M0,7 A7,7 0 1,0 14,7 M7,3l0,5 M7,9l0,2" Stroke="{DynamicResource ErrorBrush}" StrokeThickness="2"/>
</Canvas>

17
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs

@ -39,17 +39,11 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
targetType = setter.Property.PropertyType;
}
// Look upwards though the ambient context for IResourceHosts and IResourceProviders
// Look upwards though the ambient context for IResourceNodes
// which might be able to give us the resource.
foreach (var e in stack.Parents)
foreach (var parent in stack.Parents)
{
object value;
if (e is IResourceHost host && host.TryGetResource(ResourceKey, out value))
{
return ColorToBrushConverter.Convert(value, targetType);
}
else if (e is IResourceProvider provider && provider.TryGetResource(ResourceKey, out value))
if (parent is IResourceNode node && node.TryGetResource(ResourceKey, out var value))
{
return ColorToBrushConverter.Convert(value, targetType);
}
@ -58,7 +52,10 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
if (provideTarget.TargetObject is IControl target &&
provideTarget.TargetProperty is PropertyInfo property)
{
DelayedBinding.Add(target, property, x => GetValue(x, targetType));
var localTargetType = targetType;
var localInstance = this;
DelayedBinding.Add(target, property, x => localInstance.GetValue(x, localTargetType));
return AvaloniaProperty.UnsetValue;
}

6
src/Markup/Avalonia.Markup.Xaml/Styling/StyleInclude.cs

@ -60,7 +60,7 @@ namespace Avalonia.Markup.Xaml.Styling
}
}
bool IResourceNode.HasResources => (Loaded as IResourceProvider)?.HasResources ?? false;
bool IResourceNode.HasResources => Loaded?.HasResources ?? false;
IReadOnlyList<IStyle> IStyle.Children => _loaded ?? Array.Empty<IStyle>();
@ -86,9 +86,9 @@ namespace Avalonia.Markup.Xaml.Styling
public bool TryGetResource(object key, out object? value)
{
if (!_isLoading && Loaded is IResourceProvider p)
if (!_isLoading)
{
return p.TryGetResource(key, out value);
return Loaded.TryGetResource(key, out value);
}
value = null;

Loading…
Cancel
Save