Skip to content

Commit 930099a

Browse files
[XC] fix x:Static for non-int enums (#32171)
* [XC] fix x:Static for non-int enums - fixes #32056 * fix test
1 parent 4831c0e commit 930099a

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

src/Controls/src/Build.Tasks/CompiledMarkupExtensions/StaticExtension.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@ public IEnumerable<Instruction> ProvideValue(ElementNode node, ModuleDefinition
6868
return [Instruction.Create(OpCodes.Ldc_I8, (ulong)fieldDef.Constant)];
6969
if (fieldDef.Constant is uint)
7070
return [Instruction.Create(OpCodes.Ldc_I4, (uint)fieldDef.Constant)];
71-
//everything else will cast just fine to an int
71+
if (fieldDef.Constant is byte)
72+
return [Instruction.Create(OpCodes.Ldc_I4, (int)(byte)fieldDef.Constant)];
73+
if (fieldDef.Constant is sbyte)
74+
return [Instruction.Create(OpCodes.Ldc_I4, (int)(sbyte)fieldDef.Constant)];
75+
if (fieldDef.Constant is short)
76+
return [Instruction.Create(OpCodes.Ldc_I4, (int)(short)fieldDef.Constant)];
77+
if (fieldDef.Constant is ushort)
78+
return [Instruction.Create(OpCodes.Ldc_I4, (int)(ushort)fieldDef.Constant)];
79+
80+
//fallback
7281
return [Instruction.Create(OpCodes.Ldc_I4, (int)fieldDef.Constant)];
7382
}
7483
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
5+
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui32056"
6+
Title="Maui32056">
7+
<Label
8+
x:Name="label0"
9+
Text="{Binding Source={Static local:Maui32056Enum.A}, Path=.}" />
10+
</ContentPage>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.Maui.ApplicationModel;
2+
using Microsoft.Maui.Controls.Core.UnitTests;
3+
using Microsoft.Maui.Dispatching;
4+
using Microsoft.Maui.UnitTests;
5+
using NUnit.Framework;
6+
7+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
8+
9+
public enum Maui32056Enum : byte
10+
{
11+
A = 0,
12+
B = 1
13+
}
14+
15+
public partial class Maui32056 : ContentPage
16+
{
17+
public Maui32056()
18+
{
19+
InitializeComponent();
20+
}
21+
22+
[TestFixture]
23+
class Test
24+
{
25+
[SetUp]
26+
public void Setup()
27+
{
28+
Application.SetCurrentApplication(new MockApplication());
29+
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
30+
}
31+
32+
[TearDown] public void TearDown() => AppInfo.SetCurrent(null);
33+
34+
[Test]
35+
public void ByteEnum([Values] XamlInflator inflator)
36+
{
37+
if (inflator == XamlInflator.XamlC)
38+
{
39+
MockCompiler.Compile(typeof(Maui32056), out var methodDef, out var hasLoggedErrors);
40+
Assert.IsFalse(hasLoggedErrors);
41+
}
42+
var page = new Maui32056(inflator);
43+
Assert.AreEqual(Maui32056Enum.A.ToString(), page.label0.Text);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)