Skip to content

Commit

Permalink
Added support for "BasedOn" on styles in App.Xaml
Browse files Browse the repository at this point in the history
Fix #854
  • Loading branch information
carldebilly committed Jun 21, 2019
1 parent 879bf2f commit deede2b
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 41 deletions.
38 changes: 38 additions & 0 deletions src/SamplesApp/SamplesApp.Shared/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,42 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RequestedTheme="Light">

<Application.Resources>

<!-- ReSharper disable once InconsistentNaming -->
<SolidColorBrush x:Key="Pink_From_App_Xaml_Resources" Color="#FF54BA" />

<Style x:Key="Style_Bold_From_App_Xaml_Resources" TargetType="TextBlock">
<Setter Property="FontWeight" Value="ExtraBold" />
</Style>

<Style x:Key="Style_Pink_From_App_Xaml_Resources" TargetType="TextBlock" BasedOn="{StaticResource Style_Bold_From_App_Xaml_Resources}">
<Setter Property="Foreground" Value="{StaticResource Pink_From_App_Xaml_Resources}" />
</Style>

<Style x:Key="CustomTextBox" TargetType="TextBox" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid>
<Grid.Resources>
<Style x:Key="TestStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<TextBlock Text="Sample" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</Application.Resources>
</Application>
5 changes: 2 additions & 3 deletions src/SamplesApp/SamplesApp.Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ public App()
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
var sw = Stopwatch.StartNew();
var n = Windows.UI.Xaml.Window.Current.Dispatcher.RunAsync(
CoreDispatcherPriority.Idle,
() => Console.WriteLine("Done loading " + sw.Elapsed));
var n = Windows.UI.Xaml.Window.Current.Dispatcher.RunIdleAsync(
_ => Console.WriteLine("Done loading " + sw.Elapsed));

#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
Expand Down
8 changes: 8 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\Resources\XamlGlobalResources.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\UIElementTests\Arrange_Performance01.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -2117,6 +2121,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\TouchEventsTests\Touch.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\TouchEventsTests\TouchRotated.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\TouchEventsTests\TouchViewModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\Resources\XamlGlobalResources.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\UIElementTests\Arrange_Performance01.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\UIElementTests\TransformToVisual_Simple.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\VisualStateTests\VisualState_AdaptiveTrigger_Storyboard.xaml.cs" />
Expand Down Expand Up @@ -3706,6 +3711,9 @@
<Compile Update="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ToggleSwitchControl\ToggleSwitch_IsOn.xaml.cs">
<DependentUpon>ToggleSwitch_IsOn.xaml</DependentUpon>
</Compile>
<Compile Update="C:\src\Uno\src\SamplesApp\UITests.Shared\Windows_UI_Xaml\Resources\XamlGlobalResources.xaml.cs">
<DependentUpon>XamlGlobalResources.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="$(MSBuildThisFileDirectory)Assets\cart.png" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Page
x:Class="UITests.Shared.Windows_UI_Xaml.Resources.XamlGlobalResources"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid
Background="White"
BorderBrush="{StaticResource Pink_From_App_Xaml_Resources}"
BorderThickness="4"
Padding="2"
Margin="20"
VerticalAlignment="Center"
HorizontalAlignment="Center">

<TextBlock
Style="{StaticResource Style_Pink_From_App_Xaml_Resources}"
TextWrapping="WrapWholeWords"
TextAlignment="Justify"
FontSize="55">This text should be rendered in PINK with a PINK border around it.</TextBlock>

<TextBox Style="{StaticResource CustomTextBox}" />
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;
using Uno.UI.Samples.Controls;

namespace UITests.Shared.Windows_UI_Xaml.Resources
{
[SampleControlInfo("XAML", "XamlGlobalResources", description: "Using a resource defined in Global.Xaml's Resources (Pink).")]
public sealed partial class XamlGlobalResources : Page
{
public XamlGlobalResources()
{
this.InitializeComponent();
}
}
}
11 changes: 11 additions & 0 deletions src/Uno.UI/FeatureConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public static class UIElement
#endif
}

public static class Xaml
{
/// <summary>
/// Maximal "BasedOn" recursive resoling depth.
/// </summary>
/// <remarks>
/// This is a mechanism to prevent hard-to-diagnose stack overflow when a resource name is not found.
/// </remarks>
public static int MaxRecursiveResolvingDepth { get; set; } = 12;
}

public static class FrameworkElement
{
/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private void ProcessNamedMember(XamlObjectDefinition control, object instance, X
}
else if (member.Member.DeclaringType == null && member.Member.Name == "Name")
{
// This is a special case, where the declaring type is from the x: namespace,
// This is a special case, where the declaring type is from the x: namespace,
// but is considered of an unknown type. This can happen when providing the
// name of a control using x:Name instead of Name.
if (TypeResolver.GetPropertyByName(control.Type, "Name") is PropertyInfo nameInfo)
Expand Down Expand Up @@ -466,6 +466,11 @@ void ResolveResource()

_postActions.Enqueue(ResolveResource);
}
else
{
// Here we assigned a {StaticResource} on a standard property (not a DependencyProperty)
// We can't resolve it.
}
}
}

Expand Down
18 changes: 8 additions & 10 deletions src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,33 @@
namespace Windows.UI.Xaml.Markup.Reader
{
#if DEBUG
[DebuggerDisplay("Type: {_type.Name}")]
[DebuggerDisplay("Type: {Type.Name}")]
#endif
internal class XamlObjectDefinition
internal class XamlObjectDefinition
{
private XamlType _type;

public XamlObjectDefinition(XamlType type, int lineNumber, int linePosition, XamlObjectDefinition owner)
{
LineNumber = lineNumber;
LinePosition = linePosition;
_type = type;
Type = type;
Owner = owner;
Members = new List<XamlMemberDefinition>();
Objects = new List<XamlObjectDefinition>();
}

public XamlType Type { get { return _type; } }
public XamlType Type { get; }

public List<XamlMemberDefinition> Members { get; private set; }
public List<XamlMemberDefinition> Members { get; }

public List<XamlObjectDefinition> Objects { get; private set; }
public List<XamlObjectDefinition> Objects { get; }

public object Value { get; set; }

public int LineNumber { get; private set; }
public int LineNumber { get; }

public int LinePosition { get; set; }

public XamlObjectDefinition Owner { get; private set; }
public XamlObjectDefinition Owner { get; }
}

}
40 changes: 13 additions & 27 deletions src/Uno.UI/UI/Xaml/ResourceDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Foundation.Metadata;
using System.Threading;
using Uno.UI;

namespace Windows.UI.Xaml
{
public partial class ResourceDictionary : DependencyObject
{
private Dictionary<object, object> _values = new Dictionary<object, object>();
private bool _isResolving = false;
private readonly Dictionary<object, object> _values = new Dictionary<object, object>();
private int _resolvingDepth = 0;

public ResourceDictionary()
{
Expand Down Expand Up @@ -69,18 +68,19 @@ public bool TryGetValue(object key, out object value)
{
try
{
if (!_isResolving)
_resolvingDepth++;

if (_resolvingDepth <= FeatureConfiguration.Xaml.MaxRecursiveResolvingDepth)
{
// This prevents a stack overflow while looking for a
// missing resource in generated xaml code.
_isResolving = true;

value = DefaultResolver?.Invoke(key.ToString());
}
}
finally
{
_isResolving = false;
_resolvingDepth--;
}

return value != null;
Expand All @@ -102,26 +102,12 @@ public object this[object key]

return value;
}
set
{
_values[key] = value;
}
set => _values[key] = value;
}

public global::System.Collections.Generic.ICollection<object> Keys
{
get
{
return _values.Keys;
}
}
public global::System.Collections.Generic.ICollection<object> Values
{
get
{
return _values.Values;
}
}
public global::System.Collections.Generic.ICollection<object> Keys => _values.Keys;

public global::System.Collections.Generic.ICollection<object> Values => _values.Values;

public void Add(global::System.Collections.Generic.KeyValuePair<object, object> item) => _values.Add(item.Key.ToString(), item.Value);

Expand Down

0 comments on commit deede2b

Please sign in to comment.