Skip to content

[Controls] Don't let Application resources override MainPage's own resources - #40325

Merged
kubaflo merged 1 commit into
candidate/11.0.1xx-sr2from
stephanedelcroix-issue-20679-style-cascading
Nov 17, 2026
Merged

kubaflo merged 1 commit into
candidate/11.0.1xx-sr2from
stephanedelcroix-issue-20679-style-cascading

Conversation

@StephaneDelcroix

Copy link
Copy Markdown
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!

Target Branch

This PR is for Initial base
New features, API changes, or .NET 12 work (including .NET 12 fixes) main
.NET 11 servicing fixes net11, for review
Older servicing, release integration, infrastructure, or documentation The branch designated for that work

For .NET 11 fixes, only a maintainer retargets the approved PR to the active
inflight lane and merges it after CI validates that final base. Do not merge
individual fixes directly into net11; agents must not retarget or merge.

Description of Change

Setting Application.MainPage to a page that has its own implicit style let the Application's implicit style for the same type override the page's style.

Root cause: since #21879 (9.0), the MainPage setter pushes the app's merged resources with value.OnResourcesChanged(...). That skips the "own keys win" filter in VisualElement.OnParentResourcesChanged. As a result, keys that the page defines itself, such as the implicit-style key Microsoft.Maui.Controls.Button, were overwritten by the Application's values.

Fix: call value.OnParentResourcesChanged(...) instead. The Application's resources still reach the page, including theme propagation from #21879, but the page's own keys take precedence again. This is the same path Element.Parent uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3 behave correctly.

Tests

  • New StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage: fails without the fix and passes with it.
  • dotnet test src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj --filter "FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style": passed (190/190).
  • dotnet test src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj: 6734 passed and 1 failed. The failure, TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull, also fails on net11 without this change.
  • CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes fails in a filtered 150-test run with or without the fix. It passes in a smaller AppTheme-only run, so it depends on test order.

Issues Fixed

Relates to #20679. This fixes the 9.0 page-versus-Application precedence regression found while investigating that issue. The scenario in #20679 itself is by design: implicit styles from different resource levels don't merge, and CanCascade doesn't change that. The workaround is a keyed App style plus BasedOn in the page style.

@github-actions

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 -- 40325

Or

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

@azure-pipelines

This comment has been minimized.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 17, 2026
@kubaflo

kubaflo commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

/rebase

…sources

Application.MainPage pushed the app's merged resources with OnResourcesChanged,
bypassing the 'own keys win' filter in OnParentResourcesChanged. A page-level
implicit style was then replaced by the Application-level one (regressed in
9.0 by #21879). Related to #20679.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions
github-actions Bot force-pushed the stephanedelcroix-issue-20679-style-cascading branch from 8a264a9 to 8ee6fa7 Compare November 17, 2026 08:50
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@MauiBot

MauiBot commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@StephaneDelcroix — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 17, 2026
@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 17, 2026
@MauiBot

MauiBot commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@StephaneDelcroix — existing-test verification; Setup did not capture a tested commit.

Result INCONCLUSIVE Platform Android


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

Setup did not capture a verified PR head; no commit is claimed as tested.

No immutable without-fix baseline was prepared.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Nov 17, 2026

@github-actions github-actions Bot 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.

Expert Review — 1 finding

Reviewed commit 8ee6fa7.

See inline comments for details.

var button = new Button();
var page = new ContentPage
{
Resources = new ResourceDictionary

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.

[moderate] Regression Prevention and Test Coverage - Add the same precedence case with the page's implicit Button style in Resources.MergedDictionaries. That variant still loses the red border and receives the application's blue background when assigned to MainPage: ResourceDictionary.Count excludes merged dictionaries, so VisualElement.OnParentResourcesChanged takes its empty-resource fallback and forwards the application style unfiltered. Adding an unrelated direct resource does not help because Resources.Keys also excludes merged dictionaries. The direct-entry-only fixture misses this remaining instance of the bug; cover the merged-dictionary variant and include its keys in the precedence filter without resolving unused lazy values.

@kubaflo
kubaflo changed the base branch from net11 to candidate/11.0.1xx-sr2 November 17, 2026 11:09
@kubaflo
kubaflo merged commit f21bba6 into candidate/11.0.1xx-sr2 Nov 17, 2026
165 of 179 checks passed
@kubaflo
kubaflo deleted the stephanedelcroix-issue-20679-style-cascading branch November 17, 2026 11:09
kubaflo added a commit that referenced this pull request Nov 17, 2026
…sources (#40325)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

Setting `Application.MainPage` to a page that has its own implicit style
let the Application's implicit style for the same type override the
page's style.

**Root cause:** since #21879 (9.0), the `MainPage` setter pushes the
app's merged resources with `value.OnResourcesChanged(...)`. That skips
the "own keys win" filter in `VisualElement.OnParentResourcesChanged`.
As a result, keys that the page defines itself, such as the
implicit-style key `Microsoft.Maui.Controls.Button`, were overwritten by
the Application's values.

**Fix:** call `value.OnParentResourcesChanged(...)` instead. The
Application's resources still reach the page, including theme
propagation from #21879, but the page's own keys take precedence again.
This is the same path `Element.Parent` uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3
behave correctly.

**Tests**
- New
`StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage`:
fails without the fix and passes with it.
- `dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj
--filter
"FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style"`:
passed (190/190).
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`: 6734
passed and 1 failed. The failure,
`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, also
fails on `net11` without this change.
- `CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes` fails in a
filtered 150-test run with or without the fix. It passes in a smaller
AppTheme-only run, so it depends on test order.

### Issues Fixed

Relates to #20679. This fixes the 9.0 page-versus-Application precedence
regression found while investigating that issue. The scenario in #20679
itself is by design: implicit styles from different resource levels
don't merge, and `CanCascade` doesn't change that. The workaround is a
keyed App style plus `BasedOn` in the page style.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…sources (#40325)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

Setting `Application.MainPage` to a page that has its own implicit style
let the Application's implicit style for the same type override the
page's style.

**Root cause:** since #21879 (9.0), the `MainPage` setter pushes the
app's merged resources with `value.OnResourcesChanged(...)`. That skips
the "own keys win" filter in `VisualElement.OnParentResourcesChanged`.
As a result, keys that the page defines itself, such as the
implicit-style key `Microsoft.Maui.Controls.Button`, were overwritten by
the Application's values.

**Fix:** call `value.OnParentResourcesChanged(...)` instead. The
Application's resources still reach the page, including theme
propagation from #21879, but the page's own keys take precedence again.
This is the same path `Element.Parent` uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3
behave correctly.

**Tests**
- New
`StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage`:
fails without the fix and passes with it.
- `dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj
--filter
"FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style"`:
passed (190/190).
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`: 6734
passed and 1 failed. The failure,
`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, also
fails on `net11` without this change.
- `CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes` fails in a
filtered 150-test run with or without the fix. It passes in a smaller
AppTheme-only run, so it depends on test order.

### Issues Fixed

Relates to #20679. This fixes the 9.0 page-versus-Application precedence
regression found while investigating that issue. The scenario in #20679
itself is by design: implicit styles from different resource levels
don't merge, and `CanCascade` doesn't change that. The workaround is a
keyed App style plus `BasedOn` in the page style.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…sources (#40325)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

Setting `Application.MainPage` to a page that has its own implicit style
let the Application's implicit style for the same type override the
page's style.

**Root cause:** since #21879 (9.0), the `MainPage` setter pushes the
app's merged resources with `value.OnResourcesChanged(...)`. That skips
the "own keys win" filter in `VisualElement.OnParentResourcesChanged`.
As a result, keys that the page defines itself, such as the
implicit-style key `Microsoft.Maui.Controls.Button`, were overwritten by
the Application's values.

**Fix:** call `value.OnParentResourcesChanged(...)` instead. The
Application's resources still reach the page, including theme
propagation from #21879, but the page's own keys take precedence again.
This is the same path `Element.Parent` uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3
behave correctly.

**Tests**
- New
`StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage`:
fails without the fix and passes with it.
- `dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj
--filter
"FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style"`:
passed (190/190).
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`: 6734
passed and 1 failed. The failure,
`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, also
fails on `net11` without this change.
- `CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes` fails in a
filtered 150-test run with or without the fix. It passes in a smaller
AppTheme-only run, so it depends on test order.

### Issues Fixed

Relates to #20679. This fixes the 9.0 page-versus-Application precedence
regression found while investigating that issue. The scenario in #20679
itself is by design: implicit styles from different resource levels
don't merge, and `CanCascade` doesn't change that. The workaround is a
keyed App style plus `BasedOn` in the page style.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…sources (#40325)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

Setting `Application.MainPage` to a page that has its own implicit style
let the Application's implicit style for the same type override the
page's style.

**Root cause:** since #21879 (9.0), the `MainPage` setter pushes the
app's merged resources with `value.OnResourcesChanged(...)`. That skips
the "own keys win" filter in `VisualElement.OnParentResourcesChanged`.
As a result, keys that the page defines itself, such as the
implicit-style key `Microsoft.Maui.Controls.Button`, were overwritten by
the Application's values.

**Fix:** call `value.OnParentResourcesChanged(...)` instead. The
Application's resources still reach the page, including theme
propagation from #21879, but the page's own keys take precedence again.
This is the same path `Element.Parent` uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3
behave correctly.

**Tests**
- New
`StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage`:
fails without the fix and passes with it.
- `dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj
--filter
"FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style"`:
passed (190/190).
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`: 6734
passed and 1 failed. The failure,
`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, also
fails on `net11` without this change.
- `CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes` fails in a
filtered 150-test run with or without the fix. It passes in a smaller
AppTheme-only run, so it depends on test order.

### Issues Fixed

Relates to #20679. This fixes the 9.0 page-versus-Application precedence
regression found while investigating that issue. The scenario in #20679
itself is by design: implicit styles from different resource levels
don't merge, and `CanCascade` doesn't change that. The workaround is a
keyed App style plus `BasedOn` in the page style.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 19, 2026
…sources (#40325)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

Setting `Application.MainPage` to a page that has its own implicit style
let the Application's implicit style for the same type override the
page's style.

**Root cause:** since #21879 (9.0), the `MainPage` setter pushes the
app's merged resources with `value.OnResourcesChanged(...)`. That skips
the "own keys win" filter in `VisualElement.OnParentResourcesChanged`.
As a result, keys that the page defines itself, such as the
implicit-style key `Microsoft.Maui.Controls.Button`, were overwritten by
the Application's values.

**Fix:** call `value.OnParentResourcesChanged(...)` instead. The
Application's resources still reach the page, including theme
propagation from #21879, but the page's own keys take precedence again.
This is the same path `Element.Parent` uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3
behave correctly.

**Tests**
- New
`StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage`:
fails without the fix and passes with it.
- `dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj
--filter
"FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style"`:
passed (190/190).
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`: 6734
passed and 1 failed. The failure,
`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, also
fails on `net11` without this change.
- `CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes` fails in a
filtered 150-test run with or without the fix. It passes in a smaller
AppTheme-only run, so it depends on test order.

### Issues Fixed

Relates to #20679. This fixes the 9.0 page-versus-Application precedence
regression found while investigating that issue. The scenario in #20679
itself is by design: implicit styles from different resource levels
don't merge, and `CanCascade` doesn't change that. The workaround is a
keyed App style plus `BasedOn` in the page style.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 22, 2026
…sources (#40325)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

Setting `Application.MainPage` to a page that has its own implicit style
let the Application's implicit style for the same type override the
page's style.

**Root cause:** since #21879 (9.0), the `MainPage` setter pushes the
app's merged resources with `value.OnResourcesChanged(...)`. That skips
the "own keys win" filter in `VisualElement.OnParentResourcesChanged`.
As a result, keys that the page defines itself, such as the
implicit-style key `Microsoft.Maui.Controls.Button`, were overwritten by
the Application's values.

**Fix:** call `value.OnParentResourcesChanged(...)` instead. The
Application's resources still reach the page, including theme
propagation from #21879, but the page's own keys take precedence again.
This is the same path `Element.Parent` uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3
behave correctly.

**Tests**
- New
`StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage`:
fails without the fix and passes with it.
- `dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj
--filter
"FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style"`:
passed (190/190).
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`: 6734
passed and 1 failed. The failure,
`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, also
fails on `net11` without this change.
- `CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes` fails in a
filtered 150-test run with or without the fix. It passes in a smaller
AppTheme-only run, so it depends on test order.

### Issues Fixed

Relates to #20679. This fixes the 9.0 page-versus-Application precedence
regression found while investigating that issue. The scenario in #20679
itself is by design: implicit styles from different resource levels
don't merge, and `CanCascade` doesn't change that. The workaround is a
keyed App style plus `BasedOn` in the page style.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Nov 23, 2026
…sources (#40325)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

Setting `Application.MainPage` to a page that has its own implicit style
let the Application's implicit style for the same type override the
page's style.

**Root cause:** since #21879 (9.0), the `MainPage` setter pushes the
app's merged resources with `value.OnResourcesChanged(...)`. That skips
the "own keys win" filter in `VisualElement.OnParentResourcesChanged`.
As a result, keys that the page defines itself, such as the
implicit-style key `Microsoft.Maui.Controls.Button`, were overwritten by
the Application's values.

**Fix:** call `value.OnParentResourcesChanged(...)` instead. The
Application's resources still reach the page, including theme
propagation from #21879, but the page's own keys take precedence again.
This is the same path `Element.Parent` uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3
behave correctly.

**Tests**
- New
`StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage`:
fails without the fix and passes with it.
- `dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj
--filter
"FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style"`:
passed (190/190).
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`: 6734
passed and 1 failed. The failure,
`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, also
fails on `net11` without this change.
- `CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes` fails in a
filtered 150-test run with or without the fix. It passes in a smaller
AppTheme-only run, so it depends on test order.

### Issues Fixed

Relates to #20679. This fixes the 9.0 page-versus-Application precedence
regression found while investigating that issue. The scenario in #20679
itself is by design: implicit styles from different resource levels
don't merge, and `CanCascade` doesn't change that. The workaround is a
keyed App style plus `BasedOn` in the page style.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
@kubaflo kubaflo added this to the .NET 11 SR2 milestone Nov 24, 2026
KarthikRajaKalaimani pushed a commit to KarthikRajaKalaimani/maui that referenced this pull request Nov 26, 2026
…sources (dotnet#40325)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

<!--
Open .NET 11 fix PRs against net11 for review; target main for
next-major features.
After approval, a maintainer manually retargets the fix PR to the active
inflight
branch and merges it directly there. Do not merge individual fixes into
net11.
Bots must not retarget or merge. Candidate integration into net11 and
release/*
changes are separate, explicitly coordinated release work.
-->

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

Setting `Application.MainPage` to a page that has its own implicit style
let the Application's implicit style for the same type override the
page's style.

**Root cause:** since dotnet#21879 (9.0), the `MainPage` setter pushes the
app's merged resources with `value.OnResourcesChanged(...)`. That skips
the "own keys win" filter in `VisualElement.OnParentResourcesChanged`.
As a result, keys that the page defines itself, such as the
implicit-style key `Microsoft.Maui.Controls.Button`, were overwritten by
the Application's values.

**Fix:** call `value.OnParentResourcesChanged(...)` instead. The
Application's resources still reach the page, including theme
propagation from dotnet#21879, but the page's own keys take precedence again.
This is the same path `Element.Parent` uses.

Reproduced with the 9.0.120 and 10.0.41 packages. 7.0.101 and 8.0.3
behave correctly.

**Tests**
- New
`StyleTests.PageImplicitStyleWinsOverApplicationImplicitStyleWhenSetAsMainPage`:
fails without the fix and passes with it.
- `dotnet test
src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj
--filter
"FullyQualifiedName~Maui21774|FullyQualifiedName~Maui16538|FullyQualifiedName~AppTheme|FullyQualifiedName~DynamicResource|FullyQualifiedName~Style"`:
passed (190/190).
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`: 6734
passed and 1 failed. The failure,
`TwoWayBindingToNullableDoublePropertyWithEmptyStringBecomesNull`, also
fails on `net11` without this change.
- `CorrectDefaultRadioButtonThemeColorsInLightAndDarkModes` fails in a
filtered 150-test run with or without the fix. It passes in a smaller
AppTheme-only run, so it depends on test order.

### Issues Fixed

Relates to dotnet#20679. This fixes the 9.0 page-versus-Application precedence
regression found while investigating that issue. The scenario in dotnet#20679
itself is by design: implicit styles from different resource levels
don't merge, and `CanCascade` doesn't change that. The workaround is a
keyed App style plus `BasedOn` in the page style.

<!--
Verify the target branch follows the release workflow in
docs/ReleaseBranchWorkflow.md.
Infrastructure and documentation changes should target the branch where
they apply.
-->

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
kubaflo added a commit that referenced this pull request Dec 3, 2026
…ascade (#41028)

<!-- Please let the below note in for people that find this PR -->
> [!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](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Target Branch

| This PR is for | Initial base |
| --- | --- |
| New features, API changes, or .NET 12 work (including .NET 12 fixes) |
`main` |
| .NET 11 servicing fixes | `net11`, for review |
| Older servicing, release integration, infrastructure, or documentation
| The branch designated for that work |

- [x] I selected the applicable base branch using the [contribution
guide](https://github.com/dotnet/maui/blob/main/.github/CONTRIBUTING.md#choose-your-pr-base-before-starting).

For .NET 11 fixes, only a maintainer retargets the approved PR to the
active
inflight lane and merges it after CI validates that final base. Do not
merge
individual fixes directly into `net11`; agents must not retarget or
merge.

### Description of Change

**Problem (#20679):** In .NET 7, an implicit `Button` style in
`Application.Resources` and another in `ContentPage.Resources` both
appeared to apply. Since .NET 8, only the nearest one (the page style)
applies.

**Root cause:** The .NET 7 behavior was accidental. The app's implicit
style was applied while the element was still unparented, through the
`Application.Current` fallback. When the page style replaced it, those
values were never fully unapplied. The style-specificity work (#13818)
correctly unapplies replaced styles, which removed the leak. Since then,
`Style.CanCascade` has had no effect. #40325 (already in `net11`) fixed
a different Application/MainPage precedence bug and does not change
this.

**Fix:** This PR gives `Style.CanCascade` an explicit opt-in meaning for
implicit styles:

- When the nearest implicit style has `CanCascade="True"`, the implicit
styles for the **same key** further up the resource chain (parent
elements, then `Application`) are applied *underneath* it. Each one gets
a lower `SetterSpecificity` (`StyleImplicit - 1 - depth`), so nearer
styles always win.
- The cascade continues as long as each added style also has
`CanCascade`.
- An outer style is skipped when its `TargetType` doesn't apply, when it
is a base-type key without `ApplyToDerivedTypes`, or when it is already
included through `BasedOn`.
- Default behavior (`CanCascade=false`) is unchanged.

Changes:
- `MergedStyle`: computes and applies or unapplies the cascaded implicit
styles, and recomputes them when relevant resource keys change.
- `ResourcesExtensions.GetResourcesAlongParentChain`: enumerates values
for a key, nearest first, ending with `Application.Current`.
- `VisualElement` / `Element` / `ResourceDictionary`: when an ancestor
is (re)parented, a key shadowed by a local `CanCascade` style is still
forwarded to descendants, so their cascade is refreshed. XAML builds
bottom-up, so this is needed. Lazy (unmaterialized) resources are not
resolved.
- `Style.CanCascade` docs updated.

**Usage:**
```xml
<ContentPage.Resources>
    <Style TargetType="Button" CanCascade="True">
        <Setter Property="BorderColor" Value="Red" />
    </Style>
</ContentPage.Resources>
```
With this, the app-level implicit `Button` style also applies, and the
page style overrides any shared properties. Alternative without this PR:
`BasedOn="{StaticResource Microsoft.Maui.Controls.Button}"` (not
verified here).

**Known limitations:**
- Removing a key with `ResourceDictionary.Remove` doesn't raise change
notifications. This is existing behavior, so removal isn't reflected;
replacing the key is.
- Only the same key as the nearest implicit style is cascaded.

### Testing

New tests:
- `StyleTests` (Controls.Core.UnitTests):
  - `ImplicitStyleWithCanCascadeAlsoAppliesOuterImplicitStyle`
- `CascadedImplicitStylesAreUnappliedWithTheCascadingStyle` (nested
view/page/app, then replacing styles)
- `ImplicitStyleWithoutCanCascadeReplacesOuterImplicitStyle` (guards the
default behavior)
- `Maui20679` (Xaml.UnitTests): XAML repro for all inflators.

Commands and results:
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj
--filter "FullyQualifiedName~Style"`: **passed**, 207/207.
- Red check with `src/Controls/src/Core` changes stashed, filter
`FullyQualifiedName~Cascad`: **2 failed** (the two cascade tests), 91
passed.
- `dotnet test
src/Controls/tests/Core.UnitTests/Controls.Core.UnitTests.csproj`
(full): 6837 passed, 6 failed, 30 skipped. The same 6
`HostBuilderAppTests` failures (`ObjectDisposedException` in
`DispatcherProviderStub`) also fail on the unmodified `net11` baseline
(6834 passed, 6 failed), so they are unrelated.
- `Maui20679` XAML test: **not run locally**. `dotnet build
Microsoft.Maui.BuildTasks.slnf` fails in this environment with NU1605
(HarfBuzzSharp/SkiaSharp package downgrade in Resizetizer; feed access
issue). It is left for CI.

### Issues Fixed

Fixes #20679

### Let AI finish this PR

- [ ] **Let the MAUI agent address review feedback and fix remaining
issues on this PR.** Maintainers still decide whether it is merged. For
PRs from forks, the agent opens a follow-up PR that keeps your commits.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants