Skip to content

Commit

Permalink
[XC] trim x:Name values
Browse files Browse the repository at this point in the history
fixes #27121
  • Loading branch information
StephaneDelcroix authored and PureWeen committed Feb 3, 2025
1 parent aa3f986 commit c5a5f4c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Controls/src/Build.Tasks/SetFieldVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Visit(ValueNode node, INode parentNode)
{
if (!IsXNameProperty(node, parentNode))
return;
var field = Context.Body.Method.DeclaringType.Fields.SingleOrDefault(fd => fd.Name == (string)node.Value);
var field = Context.Body.Method.DeclaringType.Fields.SingleOrDefault(fd => fd.Name == ((string)node.Value).Trim());
if (field == null)
return;
Context.IL.Emit(OpCodes.Ldarg_0);
Expand Down
7 changes: 7 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui27121.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?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:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui27121">
<Label x:Name=" label0" Text =" Values: 1234" TextColor="Black"/>
</ContentPage>
44 changes: 44 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui27121.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.UnitTests;
using Mono.Cecil;
using Mono.Cecil.Cil;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui27121
{
public Maui27121() => InitializeComponent();

public Maui27121(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Test
{
[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}

[TearDown] public void TearDown() => AppInfo.SetCurrent(null);

[Test]
public void XNameAreTrimmed([Values] bool useCompiledXaml)
{
var page = new Maui27121(useCompiledXaml);
Assert.That(page.label0, Is.Not.Null);
}
}
}

0 comments on commit c5a5f4c

Please sign in to comment.