Skip to content

Commit

Permalink
fix(xamlreader): Process members on top-level resource dicitonaries
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Dec 22, 2022
1 parent bb45d4d commit 732d001
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,27 @@ public void When_Geometry()
Assert.AreEqual(FillRule.EvenOdd, geometry.FillRule);
}

[TestMethod]
public void When_ThemeResource_With_StaticResource()
{
var s = GetContent(nameof(When_ThemeResource_With_StaticResource));
var SUT = Windows.UI.Xaml.Markup.XamlReader.Load(s) as Page;

Assert.IsNotNull(SUT.Resources["Color1"]);
Assert.IsNotNull(SUT.Resources["Color2"]);
}

[TestMethod]
public void When_ResourceDictionary_With_Theme_And_Static()
{
var s = GetContent(nameof(When_ResourceDictionary_With_Theme_And_Static));
var SUT = Windows.UI.Xaml.Markup.XamlReader.Load(s) as ResourceDictionary;

Assert.AreEqual(2, SUT.ThemeDictionaries.Count);
Assert.IsNotNull(SUT["CustomSecondBrush"]);
Assert.IsNotNull(SUT["MyCustomFirstBrush"]);
}

/// <summary>
/// XamlReader.Load the xaml and type-check result.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<!--Colors-->
<!--Themed colors-->
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<Color x:Key="OnPrimaryColor">#FFFFFF</Color>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<Color x:Key="PrimaryColor">#544794</Color>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
<!--Unthemed colors-->
<Color x:Key="MyCustomFirstColor">#B910A8</Color>
<!--Custom colors brushes-->
<SolidColorBrush x:Key="CustomSecondBrush" Color="{ThemeResource CustomSecondColor}" />
<SolidColorBrush x:Key="MyCustomFirstBrush" Color="{ThemeResource MyCustomFirstColor}" />
</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Page 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"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
Name="rootPage"
mc:Ignorable="d">

<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<Color x:Key="Color1">#FFFFFF</Color>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<Color x:Key="Color1">#000000</Color>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

<Color x:Key="Color2">#B910A8</Color>
</ResourceDictionary>
</Page.Resources>

<StackPanel>
<Border Background="{StaticResource Color1}" />
<Border Background="{StaticResource Color2}" />
</StackPanel>
</Page>
20 changes: 19 additions & 1 deletion src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ public XamlObjectBuilder(XamlFileDefinition xamlFileDefinition)
}
}

if (rootInstance is null)
{
// This is a top level dictionary, where explicit members need to
// be process explicitly (other types are processed below).
foreach (var member in control.Members.Where(m => m != unknownContent))
{
ProcessNamedMember(control, rd, member, rd);
}
}

return rd;
}
else
Expand Down Expand Up @@ -582,7 +592,15 @@ private void ProcessMemberElements(object instance, XamlMemberDefinition member,
{
if (TypeResolver.IsCollectionOrListType(propertyInfo.PropertyType))
{
if (propertyInfo.PropertyType == typeof(ResourceDictionary))
if (propertyInfo.PropertyType == typeof(ResourceDictionary)
|| (
propertyInfo.DeclaringType == typeof(ResourceDictionary)
&& (
propertyInfo.Name == nameof(ResourceDictionary.ThemeDictionaries)
|| propertyInfo.Name == nameof(ResourceDictionary.MergedDictionaries)
)
)
)
{
var methods = propertyInfo.PropertyType.GetMethods();
var addMethod = propertyInfo.PropertyType.GetMethod("Add", new[] { typeof(object), typeof(object) })
Expand Down

0 comments on commit 732d001

Please sign in to comment.