Skip to content

Commit 873251d

Browse files
committed
fix: Convert resources to target property type in generated code
1 parent 33c3a26 commit 873251d

File tree

6 files changed

+100
-18
lines changed

6 files changed

+100
-18
lines changed

src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,8 +4218,6 @@ private string GetCastString(INamedTypeSymbol targetProperty, XamlObjectDefiniti
42184218
}
42194219
}
42204220

4221-
private int _staticResourceResolverOutputIndex = 0;
4222-
42234221
/// <summary>
42244222
/// Returns code for simple initialization-time retrieval for StaticResource/ThemeResource markup.
42254223
/// </summary>
@@ -4228,21 +4226,9 @@ private string GetSimpleStaticResourceRetrieval(INamedTypeSymbol? targetProperty
42284226
targetPropertyType = targetPropertyType ?? _objectSymbol;
42294227

42304228
var targetPropertyFQT = targetPropertyType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
4231-
var propertyOutputVar = $"staticResourceResolverOutputIndex{_staticResourceResolverOutputIndex++}";
4232-
4233-
var conversion = targetPropertyType.IsValueType
4234-
? $"(({targetPropertyFQT}){propertyOutputVar})"
4235-
: $"(({propertyOutputVar} as {targetPropertyFQT}) ?? default({targetPropertyFQT}))";
4236-
42374229

4238-
var staticRetrieval = $"(" +
4239-
$"global::Uno.UI.ResourceResolverSingleton.Instance.ResolveResourceStatic(" +
4240-
$"\"{keyStr}\", " +
4241-
$"out var {propertyOutputVar}, " +
4242-
$"context: {ParseContextPropertyAccess}) " +
4243-
$"? {conversion}" +
4244-
$" : default({targetPropertyFQT})" +
4245-
$")";
4230+
var staticRetrieval = $"global::Uno.UI.ResourceResolverSingleton.Instance.ResolveResourceStatic<{targetPropertyFQT}>(" +
4231+
$"\"{keyStr}\", context: {ParseContextPropertyAccess})";
42464232
TryAnnotateWithGeneratorSource(ref staticRetrieval);
42474233
return staticRetrieval;
42484234
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Windows.UI.Xaml;
6+
using static Private.Infrastructure.TestServices;
7+
8+
namespace Uno.UI.RuntimeTests.Tests.Uno_UI
9+
{
10+
public class MyCustomClass
11+
{
12+
public Duration Duration { get; set; }
13+
}
14+
15+
[TestClass]
16+
public class Given_ResourceResolver
17+
{
18+
[TestMethod]
19+
[RunsOnUIThread]
20+
public void When_Resolving_String_Resources_Should_Produce_Target_Type()
21+
{
22+
var SUT = new Resolve_String_Resources();
23+
WindowHelper.WindowContent = SUT;
24+
var expected = new Duration(TimeSpan.Parse("00:00:00.250"));
25+
26+
Assert.AreEqual(expected, SUT.ReferencingStringDurationFromDP.Duration);
27+
Assert.AreEqual(expected, SUT.ReferenceStringDurationFromProperty.Duration);
28+
}
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Page
2+
x:Class="Uno.UI.RuntimeTests.Tests.Uno_UI.Resolve_String_Resources"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="using:Uno.UI.RuntimeTests.Tests.Uno_UI"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
mc:Ignorable="d"
9+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
10+
<Page.Resources>
11+
<x:String x:Key="StringDuration">00:00:00.250</x:String>
12+
13+
<!-- Duration is a dependency property. -->
14+
<Storyboard x:Name="ReferencingStringDurationFromDP" Duration="{StaticResource StringDuration}" x:FieldModifier="public" />
15+
<local:MyCustomClass x:Name="ReferenceStringDurationFromProperty" Duration="{StaticResource StringDuration}" x:FieldModifier="public" />
16+
</Page.Resources>
17+
<Grid>
18+
19+
</Grid>
20+
</Page>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.InteropServices.WindowsRuntime;
6+
using Windows.Foundation;
7+
using Windows.Foundation.Collections;
8+
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Controls;
10+
using Windows.UI.Xaml.Controls.Primitives;
11+
using Windows.UI.Xaml.Data;
12+
using Windows.UI.Xaml.Input;
13+
using Windows.UI.Xaml.Media;
14+
using Windows.UI.Xaml.Navigation;
15+
16+
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
17+
18+
namespace Uno.UI.RuntimeTests.Tests.Uno_UI
19+
{
20+
/// <summary>
21+
/// An empty page that can be used on its own or navigated to within a Frame.
22+
/// </summary>
23+
public sealed partial class Resolve_String_Resources : Page
24+
{
25+
public Resolve_String_Resources()
26+
{
27+
this.InitializeComponent();
28+
}
29+
}
30+
}

src/Uno.UI/UI/Xaml/ResourceResolver.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,22 @@ static ResourceResolver()
6363
[EditorBrowsable(EditorBrowsableState.Never)]
6464
public static T ResolveResourceStatic<T>(object key, object context = null)
6565
{
66-
if (TryStaticRetrieval(new SpecializedResourceDictionary.ResourceKey(key), context, out var value) && value is T tValue)
66+
if (TryStaticRetrieval(new SpecializedResourceDictionary.ResourceKey(key), context, out var value))
6767
{
68-
return tValue;
68+
if (value is T tValue)
69+
{
70+
return tValue;
71+
}
72+
else
73+
{
74+
var convertedValue = BindingPropertyHelper.Convert(() => typeof(T), value);
75+
if (convertedValue is null && _log.IsEnabled(LogLevel.Warning))
76+
{
77+
_log.LogWarning($"Unable to convert value '{value}' of type '{value.GetType()}' to type '{typeof(T)}'");
78+
}
79+
80+
return (T)convertedValue;
81+
}
6982
}
7083

7184
return default(T);

src/Uno.UI/UI/Xaml/ResourceResolverSingleton.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public sealed class ResourceResolverSingleton
3030
[EditorBrowsable(EditorBrowsableState.Never)]
3131
public bool ResolveResourceStatic(object key, out object value, object context) => ResourceResolver.ResolveResourceStatic(key, out value, context);
3232

33+
[EditorBrowsable(EditorBrowsableState.Never)]
34+
public T ResolveResourceStatic<T>(object key, object context) => ResourceResolver.ResolveResourceStatic<T>(key, context);
35+
3336
[EditorBrowsable(EditorBrowsableState.Never)]
3437
public void ApplyResource(DependencyObject owner, DependencyProperty property, object resourceKey, bool isThemeResourceExtension, object context) => ResourceResolver.ApplyResource(owner, property, resourceKey, isThemeResourceExtension, context);
3538

0 commit comments

Comments
 (0)