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
22 changes: 20 additions & 2 deletions src/Controls/src/SourceGen/Visitors/CreateValuesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,25 @@ public static void CreateValue(ElementNode node, IndentedTextWriter writer, IDic
if (prop.Value is ValueNode && !node.SkipProperties.Contains(prop.Key))
node.SkipProperties.Add(prop.Key);
}


// Also skip the content property when the value is specified as a collection item.
// This handles the content property syntax like:
// <Setter Property="...">10,10,20,20</Setter>
// where the value is a collection item instead of a property.
// Without skipping this, SetPropertiesVisitor would try to process the
// collection item and generate invalid code like ".Value = ..."
// (with an empty variable name).
if (node.CollectionItems.Count == 1 && node.CollectionItems[0] is ValueNode)
Comment thread
StephaneDelcroix marked this conversation as resolved.
Resolved
{
var contentPropertyName = type.GetContentPropertyName(Context);
if (!string.IsNullOrEmpty(contentPropertyName))
{
var contentPropertyXmlName = new XmlName(node.NamespaceURI, contentPropertyName);
if (!node.SkipProperties.Contains(contentPropertyXmlName))
node.SkipProperties.Add(contentPropertyXmlName);
}
}

// Register placeholder - TryProvideValue will create the actual variable
// Use empty string as placeholder name since it will be replaced
variables[node] = new LocalVariable(type, string.Empty);
Expand All @@ -314,7 +332,7 @@ public static void CreateValue(ElementNode node, IndentedTextWriter writer, IDic
writer.WriteLine($"var {variableName} = new {type.ToFQDisplayString()}({string.Join(", ", parameters?.ToMethodParameters(writer, Context) ?? [])});");
variables[node] = new LocalVariable(type, variableName);
node.RegisterSourceInfo(Context, writer);

return;
}
}
Expand Down
133 changes: 133 additions & 0 deletions src/Controls/tests/SourceGen.UnitTests/Maui32879Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.IO;
using System.Linq;
using Xunit;

namespace Microsoft.Maui.Controls.SourceGen.UnitTests;
Comment thread
StephaneDelcroix marked this conversation as resolved.
Resolved

public class Maui32879Tests : SourceGenXamlInitializeComponentTestBase
{
[Fact]
public void StyleSetterWithAttachedPropertyContentSyntax()
{
// Test reproducing issue #32879: Style with setter on AbsoluteLayout.LayoutBounds using content property syntax
// Previously generated invalid code like:
// .Value = "10,10,20,20";
// var setter = new Setter {...};
var xaml =
"""
<?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"
x:Class="Test.TestPage">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="NetworkIndicator" TargetType="Image">
<Setter Property="AbsoluteLayout.LayoutBounds">10,10,20,20</Setter>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
</ContentPage>
""";

var code =
"""
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Test;

[XamlProcessing(XamlInflator.SourceGen)]
public partial class TestPage : ContentPage
{
public TestPage()
{
InitializeComponent();
}
}
""";

var testXamlFilePath = Path.Combine(Environment.CurrentDirectory, "Test.xaml");
var expected =
$$"""
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a .NET MAUI source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable

namespace Test;

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Maui.Controls.SourceGen, Version=10.0.0.0, Culture=neutral, PublicKeyToken=null", "10.0.0.0")]
public partial class TestPage
{
private partial void InitializeComponent()
{
// Fallback to Runtime inflation if the page was updated by HotReload
static string? getPathForType(global::System.Type type)
{
var assembly = type.Assembly;
foreach (var xria in global::System.Reflection.CustomAttributeExtensions.GetCustomAttributes<global::Microsoft.Maui.Controls.Xaml.XamlResourceIdAttribute>(assembly))
{
if (xria.Type == type)
return xria.Path;
}
return null;
}

var rlr = global::Microsoft.Maui.Controls.Internals.ResourceLoader.ResourceProvider2?.Invoke(new global::Microsoft.Maui.Controls.Internals.ResourceLoader.ResourceLoadingQuery
{
AssemblyName = typeof(global::Test.TestPage).Assembly.GetName(),
ResourcePath = getPathForType(typeof(global::Test.TestPage)),
Instance = this,
});

if (rlr?.ResourceContent != null)
{
this.InitializeComponentRuntime();
return;
}

var style1 = new global::Microsoft.Maui.Controls.Style(typeof(global::Microsoft.Maui.Controls.Image));
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(style1!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 8, 5);
var resourceDictionary = new global::Microsoft.Maui.Controls.ResourceDictionary();
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(resourceDictionary!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 7, 4);
var __root = this;
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 2, 2);
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
global::Microsoft.Maui.Controls.Internals.INameScope iNameScope = global::Microsoft.Maui.Controls.Internals.NameScope.GetNameScope(__root) ?? new global::Microsoft.Maui.Controls.Internals.NameScope();
#endif
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
global::Microsoft.Maui.Controls.Internals.NameScope.SetNameScope(__root, iNameScope);
#endif
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
global::Microsoft.Maui.Controls.Internals.INameScope iNameScope1 = new global::Microsoft.Maui.Controls.Internals.NameScope();
#endif
#line 7 "{{testXamlFilePath}}"
__root.Resources = (global::Microsoft.Maui.Controls.ResourceDictionary)resourceDictionary;
#line default
var setter = new global::Microsoft.Maui.Controls.Setter {Property = global::Microsoft.Maui.Controls.AbsoluteLayout.LayoutBoundsProperty, Value = (global::Microsoft.Maui.Graphics.Rect)new global::Microsoft.Maui.Controls.BoundsTypeConverter().ConvertFromInvariantString("10,10,20,20")!};
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(setter!) == null)
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(setter!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 9, 6);
#line 9 "{{testXamlFilePath}}"
((global::System.Collections.Generic.ICollection<global::Microsoft.Maui.Controls.Setter>)style1.Setters).Add((global::Microsoft.Maui.Controls.Setter)setter);
#line default
resourceDictionary["NetworkIndicator"] = style1;
#line 7 "{{testXamlFilePath}}"
__root.Resources = (global::Microsoft.Maui.Controls.ResourceDictionary)resourceDictionary;
#line default
}
}

""";

var (result, generated) = RunGenerator(xaml, code);
Assert.False(result.Diagnostics.Any());
Assert.Equal(expected, generated, ignoreLineEndingDifferences: true);
}
}
16 changes: 16 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui32879.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui32879">
<ContentPage.Resources>
<ResourceDictionary>
<!-- Test: Style setter with attached property AbsoluteLayout.LayoutBounds using content property syntax -->
<Style x:Key="NetworkIndicator" TargetType="Image">
<Setter Property="AbsoluteLayout.LayoutBounds">10,10,20,20</Setter>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<AbsoluteLayout>
<Image x:Name="testImage" Style="{StaticResource NetworkIndicator}" Source="dotnet_bot.png"/>
</AbsoluteLayout>
</ContentPage>
34 changes: 34 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui32879.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Maui.Graphics;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui32879 : ContentPage
{
public Maui32879() => InitializeComponent();

[TestFixture]
class Tests
{
[Test]
public void StyleSetterWithAttachedPropertyContentSyntax([Values] XamlInflator inflator)
{
// This test reproduces issue #32879:
// Style with setter on AbsoluteLayout.LayoutBounds and SourceGen fails
// The bug was that using content property syntax (value between tags)
// instead of attribute syntax generated invalid C# code like:
// .Value = "10,10,20,20";
// var setter90 = new global::Microsoft.Maui.Controls.Setter {...};
var page = new Maui32879(inflator);

Assert.That(page, Is.Not.Null);
Assert.That(page.testImage, Is.Not.Null);

// Verify the style was applied correctly
var bounds = AbsoluteLayout.GetLayoutBounds(page.testImage);
Assert.That(bounds, Is.EqualTo(new Rect(10, 10, 20, 20)));
}
}
}