Skip to content
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

[XamlC] Prioritize compiled markup extensions over primitive types #20808

Merged
merged 1 commit into from
Feb 27, 2024

Conversation

simonrozsival
Copy link
Member

Description of Change

Fixes the following build error:

Issues\Maui20616.xaml(7,10): XamlC XC0103 error : Consider attributing the markup extension "Microsoft.Maui.Controls.Xaml.TypeExtension" with [RequireService] or [AcceptEmptyServiceProvider] if it doesn't require any. [D:\a\_work\1\s\src\Controls\tests\Xaml.UnitTests\Controls.Xaml.UnitTests.csproj]

The x:Type markup extension wasn't compiled because it was recognized as IsXaml2009LanguagePrimitive. With this change, XamlC will first check if the markup extension provides a compiled variant.

Compare the output for this sample XAML:

<ContentPage.Resources>
    <x:Type x:Key="mainPage">local:MainPage</x:Type>
</ContentPage.Resources>
// decompiled IL
-TypeExtension typeExtension = new TypeExtension();
-typeExtension.TypeName = "local:MainPage";
-XamlServiceProvider xamlServiceProvider = new XamlServiceProvider();
-Type typeFromHandle = typeof(IProvideValueTarget);
-object[] array = new object[0 + 1];
-array[0] = mainPage;
-object obj;
-xamlServiceProvider.Add(typeFromHandle, obj = new SimpleValueTargetProvider(array, typeof(VisualElement).GetRuntimeProperty("Resources"), new NameScope[2] { nameScope, nameScope }, false));
-xamlServiceProvider.Add(typeof(IReferenceProvider), obj);
-Type typeFromHandle2 = typeof(IXamlTypeResolver);
-XmlNamespaceResolver xmlNamespaceResolver = new XmlNamespaceResolver();
-xmlNamespaceResolver.Add("", "http://schemas.microsoft.com/dotnet/2021/maui");
-xmlNamespaceResolver.Add("x", "http://schemas.microsoft.com/winfx/2009/xaml");
-xmlNamespaceResolver.Add("local", "clr-namespace:MyMauiApp;Assembly=MyMauiApp");
-xamlServiceProvider.Add(typeFromHandle2, new XamlTypeResolver(xmlNamespaceResolver, typeof(MainPage).Assembly));
-xamlServiceProvider.Add(typeof(IXmlLineInfoProvider), new XmlLineInfoProvider(new XmlLineInfo(7, 10)));
-Type type = ((IMarkupExtension<Type>)typeExtension).ProvideValue((IServiceProvider)xamlServiceProvider);
+Type type = typeof(MainPage);
 mainPage.Resources.Add("mainPage", type);

/cc @StephaneDelcroix @rmarinho

@simonrozsival simonrozsival requested a review from a team as a code owner February 23, 2024 12:18
@simonrozsival simonrozsival requested review from jfversluis, mattleibow, StephaneDelcroix and rmarinho and removed request for a team February 23, 2024 12:18
@StephaneDelcroix StephaneDelcroix self-assigned this Feb 23, 2024
@simonrozsival simonrozsival changed the base branch from net9-merge-main to net9.0 February 26, 2024 10:56
@rmarinho rmarinho merged commit 39a169a into dotnet:net9.0 Feb 27, 2024
45 of 47 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Mar 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants