Skip to content

Commit

Permalink
feat(xamlreader): Update Setter.Value when theme changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjohnoliver committed Nov 5, 2021
1 parent fd535d2 commit 7ba8c58
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,52 @@ public void When_StaticResource_And_NonDependencyProperty()

}

[TestMethod]
public void When_ThemeResource_And_Setter_And_Theme_Changed()
{
var app = UnitTestsApp.App.EnsureApplication();
var themeDict = new ResourceDictionary
{
ThemeDictionaries =
{
{"Light", new ResourceDictionary
{
{"MyIntResourceThemed", 244 }
}
},
{"Dark", new ResourceDictionary
{
{"MyIntResourceThemed", 9 }
}
},
}
};
app.Resources.MergedDictionaries.Add(themeDict);
try
{
var s = GetContent(nameof(When_ThemeResource_And_Setter_And_Theme_Changed));
var r = Windows.UI.Xaml.Markup.XamlReader.Load(s) as Page;

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

app.HostView.Children.Add(r);

Assert.AreEqual(ApplicationTheme.Light, app.RequestedTheme);
Assert.AreEqual(244, inner.Tag);

app.SetExplicitRequestedTheme(ApplicationTheme.Dark);
Assert.AreEqual(ApplicationTheme.Dark, app.RequestedTheme);
Assert.AreEqual(9, inner.Tag);
}
finally
{
app.SetExplicitRequestedTheme(null);
app.Resources.MergedDictionaries.Remove(themeDict);
}

}

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,17 @@
<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">
<Page.Resources>
<Style TargetType="Button">
<Setter Property="Tag"
Value="{ThemeResource MyIntResourceThemed}" />
</Style>
</Page.Resources>

<Grid x:Name="root">
<Button Content="Dummy"/>
</Grid>
</Page>
18 changes: 15 additions & 3 deletions src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.RegularExpressions;
using Uno.Extensions;
using Uno.UI;
using Uno.UI.Helpers.Xaml;
using Uno.UI.Xaml;
using Uno.Xaml;
using Windows.UI.Xaml.Controls;
Expand Down Expand Up @@ -494,9 +495,20 @@ private void ProcessStaticResourceMarkupNode(object instance, XamlMemberDefiniti
else if (propertyInfo != null)
{
GetPropertySetter(propertyInfo).Invoke(
instance,
new[] { ResourceResolver.ResolveResourceStatic(keyName, propertyInfo.PropertyType) }
);
instance,
new[] { ResourceResolver.ResolveResourceStatic(keyName, propertyInfo.PropertyType) }
);

if (instance is Setter setter && propertyInfo.Name == "Value")
{
// Register StaticResource/ThemeResource assignations to Value for resource updates
setter.ApplyThemeResourceUpdateValues(
keyName,
null,
IsThemeResourceMarkupNode(member),
true
);
}
}
}
}
Expand Down

0 comments on commit 7ba8c58

Please sign in to comment.