Skip to content

Commit

Permalink
feat(xamlreader): Support StaticResource assignation to non-DP
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjohnoliver committed Nov 5, 2021
1 parent bc4e66a commit fd535d2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,28 @@ public void When_Resources_And_Empty()
Assert.IsNotNull(r.Resources);
}

[TestMethod]
public void When_StaticResource_And_NonDependencyProperty()
{
var app = UnitTestsApp.App.EnsureApplication();
app.Resources["MyIntResource"] = 77;
try
{
var s = GetContent(nameof(When_StaticResource_And_NonDependencyProperty));
var r = Windows.UI.Xaml.Markup.XamlReader.Load(s) as Page;

var root = r.FindName("root") as Grid;
var inner = root.Children.First() as NonDependencyPropertyAssignable;

Assert.AreEqual(77, inner.MyProperty);
}
finally
{
app.Resources.Remove("MyDoubleResource");
}

}

private string GetContent(string testName)
{
var assembly = this.GetType().Assembly;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<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="Uno.UI.Tests.Windows_UI_Xaml_Markup.XamlReaderTests"
mc:Ignorable="d">

<Grid x:Name="root">
<controls:NonDependencyPropertyAssignable MyProperty="{StaticResource MyIntResource}" />
</Grid>
</Page>
20 changes: 11 additions & 9 deletions src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private void ProcessNamedMember(XamlObjectDefinition control, object instance, X
{
if (IsMarkupExtension(member))
{
ProcessMemberMarkupExtension(instance, member);
ProcessMemberMarkupExtension(instance, member, propertyInfo);
}
else
{
Expand All @@ -278,7 +278,7 @@ private void ProcessNamedMember(XamlObjectDefinition control, object instance, X
{
if (IsMarkupExtension(member))
{
ProcessMemberMarkupExtension(instance, member);
ProcessMemberMarkupExtension(instance, member, null);
}
else if (instance is DependencyObject dependencyObject)
{
Expand Down Expand Up @@ -350,7 +350,7 @@ private void ProcessTextBlock(XamlObjectDefinition control, TextBlock instance,
{
if (IsMarkupExtension(member))
{
ProcessMemberMarkupExtension(instance, member);
ProcessMemberMarkupExtension(instance, member, null);
}
else
{
Expand Down Expand Up @@ -451,19 +451,19 @@ private void ProcessMemberElements(object instance, XamlMemberDefinition member,
private static MethodInfo GetPropertySetter(PropertyInfo propertyInfo)
=> propertyInfo?.SetMethod ?? throw new InvalidOperationException($"Unable to find setter for property [{propertyInfo}]");

private void ProcessMemberMarkupExtension(object instance, XamlMemberDefinition member)
private void ProcessMemberMarkupExtension(object instance, XamlMemberDefinition member, PropertyInfo propertyInfo)
{
if (IsBindingMarkupNode(member))
{
ProcessBindingMarkupNode(instance, member);
}
else if (IsStaticResourceMarkupNode(member) || IsThemeResourceMarkupNode(member))
{
ProcessStaticResourceMarkupNode(instance, member);
ProcessStaticResourceMarkupNode(instance, member, propertyInfo);
}
}

private void ProcessStaticResourceMarkupNode(object instance, XamlMemberDefinition member)
private void ProcessStaticResourceMarkupNode(object instance, XamlMemberDefinition member, PropertyInfo propertyInfo)
{
var resourceNode = member.Objects.FirstOrDefault();

Expand Down Expand Up @@ -491,10 +491,12 @@ private void ProcessStaticResourceMarkupNode(object instance, XamlMemberDefiniti
};
}
}
else
else if (propertyInfo != null)
{
// Here we assigned a {StaticResource} on a standard property (not a DependencyProperty)
// We can't resolve it.
GetPropertySetter(propertyInfo).Invoke(
instance,
new[] { ResourceResolver.ResolveResourceStatic(keyName, propertyInfo.PropertyType) }
);
}
}
}
Expand Down

0 comments on commit fd535d2

Please sign in to comment.