Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using CommunityToolkit.Maui.UnitTests.Mocks;
using CommunityToolkit.Maui.UnitTests.Resources.Styles;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Extensions;

public class AppThemeResourceExtensionTests : BaseViewTest
{
[Fact]
public async Task SetPropertyViaAppThemeResource()
{
// Arrange
MockApplication application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
bool foundResource;

// Act
try
{
application.Resources.MergedDictionaries.Add(new AppThemeResourceDictionary());
foundResource = true;
}
catch (Exception)
{
foundResource = false;
}
// Assert
Assert.True(foundResource, "Failed to load key from AppThemeResourceDictionary. Bug 2761.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:tk="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="CommunityToolkit.Maui.UnitTests.Resources.Styles.AppThemeResourceDictionary">

<tk:AppThemeColor x:Key="TextColor" Light="Black" Dark="White" />
<tk:AppThemeColor x:Key="TextBackgroundColor" Light="White" Dark="Black" />

<Style TargetType="Entry">
<Setter Property="TextColor" Value="{tk:AppThemeResource TextColor}" />
</Style>

</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CommunityToolkit.Maui.UnitTests.Resources.Styles;

public partial class AppThemeResourceDictionary : ResourceDictionary
{
public AppThemeResourceDictionary()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,32 @@ public BindingBase ProvideValue(IServiceProvider serviceProvider)
}
}

// Fallback to root object ResourceDictionary (e.g. page-level resources)
// Fallback to root object ResourceDictionary (e.g. page-level or application-level resources)
var rootProvider = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
var root = rootProvider?.RootObject;
if (root is IResourcesProvider { IsResourcesCreated: true } rootResources
&& rootResources.Resources.TryGetValue(Key, out resource))

switch (root)
{
switch (resource)
{
case AppThemeColor rootColor:
return rootColor.GetBinding();
case AppThemeObject rootTheme:
return rootTheme.GetBinding();
default:
var info = (serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider)?.XmlLineInfo;
throw new XamlParseException($"Resource found for key {Key} is not a valid AppTheme resource.", info);
}
case IResourcesProvider { IsResourcesCreated: true } rootResources1 when rootResources1.Resources.TryGetValue(Key, out resource):
// page level?
switch (resource)
{
case AppThemeColor rootColor:
return rootColor.GetBinding();
case AppThemeObject rootTheme:
return rootTheme.GetBinding();
}
break;
case ResourceDictionary rootDictionary1 when rootDictionary1.TryGetValue(Key, out resource):
// application level?
switch (resource)
{
case AppThemeColor rootColor:
return rootColor.GetBinding();
case AppThemeObject rootTheme:
return rootTheme.GetBinding();
}
break;
}

if (Application.Current?.Resources.TryGetValueAndSource(Key, out resource, out _) is true)
Expand Down
Loading