Skip to content

Commit 658b3f0

Browse files
committed
fix: Align resource conversion for empty values
1 parent cb55ebd commit 658b3f0

File tree

8 files changed

+139
-18
lines changed

8 files changed

+139
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4818,9 +4818,9 @@ string Inner()
48184818

48194819
if (_resourceKeys.Any(k => k == fullKey))
48204820
{
4821-
var resourceNameString = resourceFileName == null ? "" : $"\"{resourceFileName}\"";
4821+
var resourceNameString = resourceFileName == null ? "null" : $"\"{resourceFileName}\"";
48224822

4823-
return $"global::Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView({resourceNameString}).GetString(\"{fullKey}\")";
4823+
return $"global::Uno.UI.Helpers.MarkupHelper.GetResourceStringForXUid({resourceNameString}, \"{fullKey}\")";
48244824
}
48254825

48264826
return null;

src/Uno.UI.Tests/ResourceLoader/Given_ResourceLoader.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public void When_ResourceFile_Neutral()
3737
Assert.AreEqual("App70-en", _ResourceLoader.GetForCurrentView().GetString("ApplicationName"));
3838
}
3939

40+
[TestMethod]
41+
public void When_Empty_Resource()
42+
{
43+
_ResourceLoader.DefaultLanguage = "en";
44+
45+
Assert.AreEqual("", _ResourceLoader.GetForCurrentView().GetString("TestEmptyResource"));
46+
}
47+
4048
[TestMethod]
4149
public void When_ResourceFile_Neutral_Both()
4250
{

src/Uno.UI.Tests/ResourceLoader/Strings/en/Resources.Designer.cs

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Uno.UI.Tests/ResourceLoader/Strings/en/Resources.resw

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120-
<data name="ApplicationName" xml:space="preserve">
121-
<value>App70-en</value>
120+
<data name="TestEmptyConverterText.ValueIfNotNull" xml:space="preserve">
121+
<value />
122122
</data>
123-
<data name="Given_ResourceLoader.When_LocalizedResource" xml:space="preserve">
124-
<value>Text in 'en'</value>
123+
<data name="TestEmptyConverterText.ValueIfNull" xml:space="preserve">
124+
<value>Test</value>
125125
</data>
126-
<data name="ThemeRadioButtons.Header" xml:space="preserve">
127-
<value>Header in 'en'</value>
126+
<data name="TestEmptyResource" xml:space="preserve">
127+
<value />
128128
</data>
129129
</root>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<UserControl x:Class="Uno.UI.Tests.Windows_UI_Xaml_Markup.XUidTests.Controls.When_Empty_XUid"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="using:Uno.UI.Tests.Windows_UI_Xaml_Markup.XUidTests.Controls"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d">
8+
9+
<UserControl.Resources>
10+
<local:TestEmptyConverter x:Key="TestEmptyConverterText"
11+
x:Uid="TestEmptyConverterText" />
12+
13+
</UserControl.Resources>
14+
15+
</UserControl>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
namespace Uno.UI.Tests.Windows_UI_Xaml_Markup.XUidTests.Controls
17+
{
18+
public sealed partial class When_Empty_XUid : UserControl
19+
{
20+
public When_Empty_XUid()
21+
{
22+
this.InitializeComponent();
23+
}
24+
}
25+
26+
public class TestEmptyConverter : IValueConverter
27+
{
28+
public TestEmptyConverter()
29+
{
30+
ValueIfNull = null;
31+
ValueIfNotNull = null;
32+
}
33+
34+
public object ValueIfNull { get; set; }
35+
36+
public object ValueIfNotNull { get; set; }
37+
38+
public object Convert(object value, Type targetType, object parameter, string language) => throw new NotImplementedException();
39+
public object ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException();
40+
}
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using Uno.UI.Tests.App.Xaml;
8+
using Uno.UI.Tests.Helpers;
9+
using Windows.Foundation;
10+
using Windows.UI;
11+
using Windows.UI.Xaml;
12+
using Windows.UI.Xaml.Controls;
13+
using Windows.UI.Xaml.Media;
14+
using _ResourceLoader = Windows.ApplicationModel.Resources.ResourceLoader;
15+
using Uno.UI.Helpers;
16+
using Uno.UI.Tests.Windows_UI_Xaml_Markup.XUidTests.Controls;
17+
18+
namespace Uno.UI.Tests.Windows_UI_Xaml_Markup.XUidTests
19+
{
20+
[TestClass]
21+
public class Given_xUid
22+
{
23+
[TestInitialize]
24+
public void Init()
25+
{
26+
UnitTestsApp.App.EnsureApplication();
27+
}
28+
29+
[TestMethod]
30+
public void When_EmptyXUid()
31+
{
32+
var SUT = new When_Empty_XUid();
33+
var conv = (TestEmptyConverter)SUT.Resources["TestEmptyConverterText"];
34+
35+
Assert.IsNull(conv.ValueIfNotNull);
36+
Assert.AreEqual("Test", conv.ValueIfNull);
37+
}
38+
}
39+
}

src/Uno.UI/Helpers/MarkupHelper.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using Uno.UI.DataBinding;
88
using Uno.UI.Xaml.Markup;
9+
using Windows.ApplicationModel.Resources;
910
using Windows.UI.Xaml;
1011
using Windows.UI.Xaml.Markup;
1112

@@ -37,6 +38,23 @@ public static void SetXUid(object target, string uid)
3738
public static string GetXUid(object target)
3839
=> target is IXUidProvider provider ? provider.Uid : "";
3940

41+
/// <summary>
42+
/// Gets a resource string for an x:Uid bound property.
43+
/// </summary>
44+
/// <remarks>
45+
/// Returns null when the resource is an empty string.
46+
/// </remarks>
47+
public static string? GetResourceStringForXUid(string viewName, string resourceName)
48+
{
49+
var loader = viewName is not null
50+
? ResourceLoader.GetForCurrentView(viewName)
51+
: ResourceLoader.GetForCurrentView();
52+
53+
return loader.GetString(resourceName) is { Length: > 0 } value
54+
? value
55+
: null;
56+
}
57+
4058
/// <summary>
4159
/// Sets a builder for markup-lazy properties in <see cref="VisualState"/>
4260
/// </summary>

0 commit comments

Comments
 (0)