Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[X] Support DynResources as AppThemeBinding values #16597

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Controls/src/Core/AppThemeBinding.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable disable
using System;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Internals;

namespace Microsoft.Maui.Controls
{
Expand Down Expand Up @@ -61,7 +62,13 @@ void ApplyCore(bool dispatch = false)
else
Set();

void Set() => target.SetValueCore(_targetProperty, GetValue(), Internals.SetValueFlags.ClearDynamicResource, BindableObject.SetValuePrivateFlags.Default | BindableObject.SetValuePrivateFlags.Converted, specificity);
void Set() {
var value = GetValue();
if (value is DynamicResource dynamicResource)
target.SetDynamicResource(_targetProperty, dynamicResource.Key, specificity);
else
target.SetValueCore(_targetProperty, value, Internals.SetValueFlags.ClearDynamicResource, BindableObject.SetValuePrivateFlags.Default | BindableObject.SetValuePrivateFlags.Converted, specificity);
};
}

object _light;
Expand Down
20 changes: 20 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui13619.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui13619">
<ContentPage.Resources>
<Color x:Key="Primary">DarkGray</Color>
<Color x:Key="Secondary">HotPink</Color>
<Color x:Key="Tertiary">Yellow</Color>
<Style TargetType="Label" x:Key="labelStyle" >
<Setter Property="Padding" Value="0"/>
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Secondary}, Dark={StaticResource Primary}}" />
<Setter Property="BackgroundColor" Value="{AppThemeBinding Light={DynamicResource Primary}, Dark={DynamicResource Secondary}}" />
</Style>
</ContentPage.Resources>
<Label x:Name="label0"
Style="{StaticResource labelStyle}"/>
</ContentPage>
39 changes: 39 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui13619.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Devices;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests
{


public partial class Maui13619 : ContentPage
{
public Maui13619() => InitializeComponent();
public Maui13619(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Tests
{
[SetUp] public void Setup() => AppInfo.SetCurrent(new MockAppInfo());
[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void AppThemeBindingAndDynamicResource([Values(false, true)] bool useCompiledXaml)
{
var page = new Maui13619(useCompiledXaml);
Assert.That(page.label0.TextColor, Is.EqualTo(Colors.HotPink));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you also went to the Barbie movie this weekend!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can differentiate about 9 colors, HotPink is 2 of them.

Assert.That(page.label0.BackgroundColor, Is.EqualTo(Colors.DarkGray));

page.Resources["Primary"] = Colors.SlateGray;
Assert.That(page.label0.BackgroundColor, Is.EqualTo(Colors.SlateGray));

}
}
}
}
Loading