Skip to content

Conversation

@StephaneDelcroix
Copy link
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

Fixes XSG (XAML Source Generator) handling of RelativeSource with AncestorType={x:Type ...}.

Issue:
When using {RelativeSource AncestorType={x:Type vm:Foo}} inside a DataTemplate, XSG would fail with:

error MAUIG1001: An error occurred while parsing Xaml: Invalid RelativeSource Mode '(none)'.

Root causes:

  1. The sample file SwipeViewBindingContextGallery.xaml had an incorrect clr-namespace (Microsoft.Maui.Controls.ControlGallery... instead of Maui.Controls.Sample...), which caused the x:Type to fail resolution
  2. XSG's ProvideValueForRelativeSourceExtension wasn't checking all namespace variations for the AncestorType property

Changes

  • KnownMarkups.cs:

    • Improve x:Type resolution in ProvideValueForRelativeSourceExtension to resolve inline when not cached
    • Check empty, null, and MauiUri namespace variations for AncestorType property
  • SwipeViewBindingContextGallery.xaml:

    • Fix xmlns:vm to use correct namespace (Maui.Controls.Sample.Pages.SwipeViewGalleries)
  • SwipeViewBindingContextGallery.xaml.cs:

    • Remove [XamlCompilation(XamlCompilationOptions.Skip)] since file now compiles correctly
  • Tests:

    • Add Maui33876.xaml test for RelativeSource inside DataTemplate

Fixes #33876

- Improve ProvideValueForRelativeSourceExtension to resolve x:Type inline
  when it's not yet cached in context.Types
- Check all namespace variations for AncestorType property (empty, null, MauiUri)
- Fix SwipeViewBindingContextGallery sample to use correct namespace
  (was using Microsoft.Maui.Controls.ControlGallery... instead of Maui.Controls.Sample...)
- Remove [XamlCompilation(Skip)] from SwipeViewBindingContextGallery since it now compiles
- Add unit test for RelativeSource inside DataTemplate

Fixes #33876
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes XSG (XAML Source Generator) handling of RelativeSource bindings with AncestorType={x:Type ...} syntax. Previously, XSG would fail with error "Invalid RelativeSource Mode '(none)'" when encountering this pattern inside DataTemplates.

Changes:

  • Improved x:Type resolution in XSG to handle cases where x:Type extensions are not yet cached
  • Fixed incorrect namespace in SwipeViewBindingContextGallery.xaml sample
  • Added test coverage for RelativeSource inside DataTemplate scenarios

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Controls/src/SourceGen/KnownMarkups.cs Enhanced ProvideValueForRelativeSourceExtension to resolve x:Type inline when not cached, and checks multiple namespace variations for AncestorType property
src/Controls/tests/Xaml.UnitTests/Issues/Maui33876.xaml Test XAML demonstrating RelativeSource with AncestorType inside a DataTemplate
src/Controls/tests/Xaml.UnitTests/Issues/Maui33876.xaml.cs Test code-behind with test case for the RelativeSource fix
src/Controls/samples/Controls.Sample/Pages/Controls/SwipeViewGalleries/SwipeViewBindingContextGallery.xaml Fixed incorrect namespace from Microsoft.Maui.Controls.ControlGallery.GalleryPages.SwipeViewGalleries to correct Maui.Controls.Sample.Pages.SwipeViewGalleries
src/Controls/samples/Controls.Sample/Pages/Controls/SwipeViewGalleries/SwipeViewBindingContextGallery.xaml.cs Removed [XamlCompilation(XamlCompilationOptions.Skip)] and unused using statement since file now compiles correctly

{
public Maui33876() => InitializeComponent();

[Collection("Issue")]
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the XAML Unit Testing Guidelines, test classes should use [Collection("Issue")] instead of [TestFixture]. [TestFixture] is a NUnit attribute, but this codebase uses xUnit for XAML unit tests. All other test files in the Issues directory consistently use [Collection("Issue")].

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +17 to +18
[Theory]
[XamlInflatorData]
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the XAML Unit Testing Guidelines, test methods should use [Theory] with [XamlInflatorData] instead of [Test] with [Values] XamlInflator inflator. The correct pattern is used throughout the codebase, as seen in other recent tests like Maui32697.xaml.cs and Maui33676.xaml.cs.

Copilot generated this review using guidance from repository custom instructions.
{
// x:Type extension not yet processed - resolve it now
// This can happen when RelativeSource is processed before the x:Type child
if (!typeExtNode.Properties.TryGetValue(new XmlName("", "TypeName"), out INode? typeNameNode)
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TypeName property check is missing a null namespace variant. Other property lookups in this method (Mode at lines 138-139, AncestorType at lines 152-153, AncestorLevel at lines 202-203) check for both empty string and null namespace variants. The new code should check for new XmlName(null, "TypeName") as well to be consistent with the rest of the codebase and handle all possible namespace variations.

Suggested change
if (!typeExtNode.Properties.TryGetValue(new XmlName("", "TypeName"), out INode? typeNameNode)
if (!typeExtNode.Properties.TryGetValue(new XmlName("", "TypeName"), out INode? typeNameNode)
&& !typeExtNode.Properties.TryGetValue(new XmlName(null, "TypeName"), out typeNameNode)

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[XSG] RelativeSource without explicit Mode parsed as invalid '(none)'

2 participants