Repository navigation
[XSG] Fix Style setter with content syntax in SourceGen #32890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
StephaneDelcroix
merged 6 commits into
main
from
copilot/fix-absolute-layout-style-error
Nov 28, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93a912e
Initial plan
Copilot efdd8cf
Fix Style setter with attached property content syntax in SourceGen
Copilot 87d97c7
Format code according to repository standards
Copilot 2422c6a
Add SourceGen unit tests for issue #32879
Copilot 94d4fe1
Address PR feedback: remove debug logging in SourceGen tests
Copilot e088fce
Convert SourceGen test to snapshot test with expected output
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/Controls/tests/SourceGen.UnitTests/Maui32879Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
34
src/Controls/tests/Xaml.UnitTests/Issues/Maui32879.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.