Skip to content

[net11.0] Trimmable StyleSheets#33160

Merged
jfversluis merged 7 commits into
net11.0from
dev/simonrozsival/css-trimmable
Mar 20, 2026
Merged

[net11.0] Trimmable StyleSheets#33160
jfversluis merged 7 commits into
net11.0from
dev/simonrozsival/css-trimmable

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Description of Change

This PR will allow trimming CSS-related code and default styles when there are no CSS stylesheets used in the MAUI app.

  • When @(MauiCss) item is added, the CSS feature is enabled
  • When the app does not have any @(MauiCss) but devs want to use Style.Parse to parse styles at runtime, they must enable $(MauiCssEnabled)=true

Most of the changes in this PR are around moving assembly-level attributes into a local list. This will allow us to shave of some unnecessary code from most apps:

// before
[assembly: StyleProperty("background-color", typeof(VisualElement), nameof(VisualElement.BackgroundColorProperty))]

// after
new StylePropertyAttribute("background-color", typeof(VisualElement), nameof(VisualElement.BackgroundColorProperty)),

Related to #29952

Copilot AI review requested due to automatic review settings December 15, 2025 12:33
@simonrozsival simonrozsival added the perf/app-size Application Size / Trimming (sub: perf) label Dec 15, 2025
@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 introduces a trimming optimization for CSS-related code in .NET MAUI applications. The feature allows CSS stylesheets code to be trimmed away when not used, reducing app size. The main approach involves moving assembly-level StylePropertyAttribute declarations from AssemblyInfo.cs into a local array in Registrar.cs, which enables better trimming behavior. A new runtime feature flag IsCssEnabled controls whether CSS functionality is available at runtime, with MSBuild logic automatically disabling it in trimmed builds when no CSS files are present.

Key Changes

  • Adds IsCssEnabled runtime feature flag with default value of true
  • Moves 100+ assembly-level StyleProperty attributes into a local array to enable trimming
  • Adds runtime checks that throw NotSupportedException when CSS is disabled but CSS APIs are called

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Core/src/RuntimeFeature.cs Adds IsCssEnabled feature flag with default value of true and feature switch definition for .NET 9+
src/Controls/src/Core/VisualElement/VisualElement_StyleSheet.cs Adds runtime check in GetProperty to throw helpful error when CSS is disabled
src/Controls/src/Core/StyleSheets/Style.cs Adds runtime checks in Parse and Apply methods to prevent CSS usage when disabled
src/Controls/src/Core/Registrar.cs Converts 100+ assembly-level attributes to local array and adds early return when CSS is disabled
src/Controls/src/Core/Properties/AssemblyInfo.cs Removes all assembly-level StylePropertyAttribute declarations and unused using statements
src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.targets Sets MauiCssEnabled=false in trimmed builds when no @(MauiCss) items exist, and configures runtime option

Comment thread src/Controls/src/Core/Registrar.cs Outdated
new StylePropertyAttribute("width", typeof(VisualElement), nameof(VisualElement.WidthRequestProperty)),
new StylePropertyAttribute("letter-spacing", typeof(ITextElement), nameof(TextElement.CharacterSpacingProperty)) { Inherited = true },
#pragma warning disable CS0618 // Type or member is obsolete
new StylePropertyAttribute("line-height", typeof(ILineHeightElement), nameof(LineHeightElement.LineHeightProperty)) { Inherited = true },

Copilot AI Dec 15, 2025

Copy link

Choose a reason for hiding this comment

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

The type reference for ILineHeightElement in this attribute is incorrect. Since this code is in the Microsoft.Maui.Controls.Internals namespace, typeof(ILineHeightElement) will resolve to the internal obsolete Microsoft.Maui.Controls.Internals.ILineHeightElement instead of the public Microsoft.Maui.Controls.ILineHeightElement that was used in the original assembly attribute. This needs to be changed to use the fully qualified type name or add a using alias to ensure the correct type is referenced.

Suggested change
new StylePropertyAttribute("line-height", typeof(ILineHeightElement), nameof(LineHeightElement.LineHeightProperty)) { Inherited = true },
new StylePropertyAttribute("line-height", typeof(Microsoft.Maui.Controls.ILineHeightElement), nameof(LineHeightElement.LineHeightProperty)) { Inherited = true },

Copilot uses AI. Check for mistakes.
@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run maui-pr-devicetests,maui-pr-uitests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
Member Author

Follow-up reminder: Documentation update needed

After this PR is merged, we need to document the MauiCssEnabled feature switch in the official .NET MAUI docs at dotnet/maui-docs.

The feature switch documentation should explain:

  • CSS is automatically disabled when no MauiCss items exist in the project
  • How to manually enable CSS with <MauiCssEnabled>true</MauiCssEnabled>
  • Trimming benefits when CSS is disabled

The in-repo documentation has been updated in docs/design/FeatureSwitches.md.

@simonrozsival simonrozsival force-pushed the dev/simonrozsival/css-trimmable branch from 3c28640 to db2f075 Compare January 16, 2026 07:03
@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run maui-pr-uitests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run maui-pr-devicetests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@rmarinho

Copy link
Copy Markdown
Member

/rebase

@github-actions github-actions Bot force-pushed the dev/simonrozsival/css-trimmable branch from db2f075 to 74375c4 Compare January 16, 2026 13:55
@rmarinho

Copy link
Copy Markdown
Member

/azp run maui-pr-devicetests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival simonrozsival changed the title [net11.0] Trim CSS-related code when not used [net11.0] Trimmable StyleSheets Jan 20, 2026
@simonrozsival simonrozsival force-pushed the dev/simonrozsival/css-trimmable branch from 22761a2 to 77c461a Compare January 20, 2026 13:33
@rmarinho

Copy link
Copy Markdown
Member

/rebase

@github-actions github-actions Bot force-pushed the dev/simonrozsival/css-trimmable branch from 77c461a to 7ded7f1 Compare January 31, 2026 01:40
@simonrozsival

Copy link
Copy Markdown
Member Author

@copilot this PR needs a proper rebase with conflict resolution. open a PR which addresses this issue. make sure it is up to date with the base net11.0 branch.

Copilot AI commented Feb 12, 2026

Copy link
Copy Markdown
Contributor

@simonrozsival I've opened a new pull request, #34010, to work on those changes. Once the pull request is ready, I'll request review from you.

@simonrozsival simonrozsival force-pushed the dev/simonrozsival/css-trimmable branch from 7ded7f1 to 66d1243 Compare February 13, 2026 08:09
simonrozsival added a commit that referenced this pull request Feb 18, 2026
…hanges

These changes belong in separate PRs:
- #33611 (CSS StyleSheet trimming)
- #33561 (EventTrigger trimming)
- #33160 (HybridWebView trimming)

Reverted files to their main branch state, keeping only the
[ElementHandler] attribute addition on HybridWebView.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@StephaneDelcroix StephaneDelcroix force-pushed the dev/simonrozsival/css-trimmable branch from 66d1243 to 564c244 Compare February 27, 2026 15:24
@github-actions

github-actions Bot commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 33160

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 33160"

@simonrozsival simonrozsival force-pushed the dev/simonrozsival/css-trimmable branch from 564c244 to afbda13 Compare March 11, 2026 09:31
simonrozsival and others added 7 commits March 20, 2026 10:07
- Add MauiCssEnabled=true to TestCases.HostApp.csproj since some UI tests use CSS functionality
- Add unit tests for CSS-disabled scenarios in StyleTests.cs
- Update NotSupportedException messages to explain why CSS is disabled and how to enable it
- Add MauiCssEnabled documentation to docs/design/FeatureSwitches.md
- Added follow-up reminder comment to PR for dotnet/maui-docs update
typeof(ILineHeightElement) was resolving to the obsolete
Microsoft.Maui.Controls.Internals.ILineHeightElement. Use fully
qualified Microsoft.Maui.Controls.ILineHeightElement and remove
the CS0618 pragma suppression.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- IsCssEnabled: use FeatureSwitchPrefix + nameof() to match other
  feature switches in RuntimeFeature.cs
- FeatureSwitches.md: clarify that CSS auto-disable only applies to
  optimized builds (PublishAot=true or TrimMode=full)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simonrozsival simonrozsival force-pushed the dev/simonrozsival/css-trimmable branch from 96755c5 to fa6afda Compare March 20, 2026 09:08
@simonrozsival

Copy link
Copy Markdown
Member Author

@rmarinho @jfversluis could you please have a look at this PR and let me know what you think about it? could we merge this into net11.0 soon?

@jfversluis jfversluis added this to the .NET 11.0-preview3 milestone Mar 20, 2026
@jfversluis jfversluis merged commit 7d39822 into net11.0 Mar 20, 2026
29 checks passed
@jfversluis jfversluis deleted the dev/simonrozsival/css-trimmable branch March 20, 2026 14:37
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

perf/app-size Application Size / Trimming (sub: perf)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants