Skip to content

Defer unresolved BasedOn static resources in loading resource dictionaries - #40103

Merged
kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
MykhailoDav:fix-24667-global-style-basedon
Nov 10, 2026
Merged

kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
MykhailoDav:fix-24667-global-style-basedon

Conversation

@MykhailoDav

Copy link
Copy Markdown
Contributor

Description of Change

When a ResourceDictionary is inflated at runtime (for example a style dictionary without code-behind loaded through Source=), a BasedOn="{StaticResource …}" lookup can only see resources already added in that same file, plus application resources that already exist. A base style living in a sibling merged dictionary is not reachable yet, so loading the dictionary throws XamlParseException: StaticResource not found for key ButtonBaseStyle and crashes the app at startup. Declaring a global (implicit) style based on a shared keyed style this way is the scenario from the issue.

Fix

In StaticResourceExtension.ProvideValue, when the unresolved target is Style.BasedOn and the XAML root being parsed is a ResourceDictionary (one that is still being loaded), defer the lookup to apply time through the existing Style.BaseResourceKey mechanism instead of throwing. The base style is then resolved through the consuming element's resource scope, where the merged dictionaries are available. Failed lookups anywhere else keep throwing exactly as before.

Tests

Maui24667 (Xaml.UnitTests) inflates a page whose resources merge two source dictionaries — a keyed ButtonBaseStyle and an implicit Button style based on it — plus the issue's implicit-BaseResourceKey snippet, and verifies the base setters are applied. The test runs on all three inflators (Runtime, XamlC, SourceGen).

Without fix With fix
Implicit style with BasedOn="{StaticResource …}" to a keyed style in a sibling merged dictionary XamlParseException: StaticResource not found for key ButtonBaseStyle Style applied, base setters inherited
Implicit style with BaseResourceKey (issue snippet) works works
Full Xaml.UnitTests + Core.UnitTests — No new failures

Issues Fixed

Fixes #24667

@github-actions

github-actions Bot commented Nov 9, 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 -- 40103

Or

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

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Nov 9, 2026
@azure-pipelines

This comment has been minimized.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@MykhailoDav! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @MykhailoDav! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@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 9, 2026
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@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 — 2 findings

Reviewed commit abe47b7.

See inline comments for details.

&& serviceProvider.GetService(typeof(IRootObjectProvider)) is IRootObjectProvider rootProvider
&& rootProvider.RootObject is ResourceDictionary)
{
style.BaseResourceKey = Key;

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.

[major] Logic and Correctness Verification - Converting this StaticResource to BaseResourceKey changes which base style is selected. With the new sibling-dictionary fixture, add a Button-local resource named ButtonBaseStyle with Padding=99: Style.Apply calls SetDynamicResource, whose lookup starts at the consuming Button, so it selects 99 instead of the sibling dictionary's 10,7. Keeping the base in the referencing dictionary resolves it statically and selects 10,7. Resolve the deferred reference against the completed dictionary scope rather than changing it into a per-element dynamic resource.

&& rootProvider.RootObject is ResourceDictionary)
{
style.BaseResourceKey = Key;
return null;

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] Logic and Correctness Verification - This return also permanently suppresses genuinely missing base-style errors. Rename the new Rd2 reference to ButonBaseStyle without defining that key anywhere: loading now succeeds and the Button silently loses its base setters, because Element.OnSetDynamicResource does nothing when lookup fails. During Hot Reload this also bypasses ExceptionHandler2, dropping the missing-resource diagnostic preserved by #35020. Preserve error reporting when deferred lookup still cannot resolve the key, and add a negative missing-base regression case; the new fixture always supplies the key.

@MauiBot

MauiBot commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@MykhailoDav — existing-test verification for commit abe47b72.

Result PASSED Platform Android Commit abe47b72


🧪 Gate analysis — click to expand

✅ Passed. The selected verification satisfied the gate's passing conditions. Unmatched, skipped, or inconclusive groups are not passing evidence.

📄 XAML unit tests
Test group Without fix With fix
Maui24667 ✅ Failed as expected (114s) ✅ Passed (44s)
Maui24667Rd1 ⚠ No matching tests ⚠ No matching tests
Maui24667Rd2 ⚠ No matching tests ⚠ No matching tests
Execution log excerpts

Without fix Maui24667

   at Microsoft.Maui.Controls.Xaml.UnitTests.Maui24667..ctor(XamlInflator inflator) in /_/src/Controls/tests/Xaml.UnitTests/Generated/Microsoft.Maui.Controls.SourceGen/Microsoft.Maui.Controls.SourceGen.XamlGenerator/Issues_Maui24667.xaml.sg.cs:line 71
   at Microsoft.Maui.Controls.Xaml.UnitTests.Maui24667.Tests.ImplicitStyleBasedOnKeyedStyle(XamlInflator inflator) in /_/src/Controls/tests/Xaml.UnitTests/Issues/Maui24667.xaml.cs:line 29
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
   at Microsoft.Maui.Controls.Xaml.UnitTests.Maui24667.InitializeComponentXamlC() in /_/src/Controls/tests/Xaml.UnitTests/Generated/Microsoft.Maui.Controls.SourceGen/Microsoft.Maui.Controls.SourceGen.XamlGenerator/Issues_Maui24667.xaml.sg.cs:line 43
   at Microsoft.Maui.Controls.Xaml.UnitTests.Maui24667..ctor(XamlInflator inflator) in /_/src/Controls/tests/Xaml.UnitTests/Generated/Microsoft.Maui.Controls.SourceGen/Microsoft.Maui.Controls.SourceGen.XamlGenerator/Issues_Maui24667.xaml.sg.cs:line 68
   at InvokeStub_Tests.ImplicitStyleBasedOnKeyedStyle(Object, Span`1)
   at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at Microsoft.Maui.Controls.ResourceDictionary.<>c__DisplayClass13_0.<GetOrCreateInstance>b__0(Type _) in /_/src/Controls/src/Core/ResourceDictionary.cs:line 97
   at Microsoft.Maui.Controls.ResourceDictionary.GetOrCreateInstance(Type type) in /_/src/Controls/src/Core/ResourceDictionary.cs:line 89
   at Microsoft.Maui.Controls.Xaml.ResourceDictionaryHelpers.SetAndLoadSource(ResourceDictionary rd, Uri value, String resourcePath, Assembly assembly, IXmlLineInfo lineInfo) in /_/src/Controls/src/Xaml/ResourceDictiona

With fix Maui24667

  Determining projects to restore...
  All projects are up-to-date for restore.
Test run for /home/vsts/work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net11.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll (.NETCoreApp,Version=v11.0)
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.01] xUnit.net VSTest Adapter v2.8.2+699d445a1a (64-bit .NET 11.0.0)
[xUnit.net 00:00:00.81]   Discovering: Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:11.06]   Discovered:  Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:11.09]   Starting:    Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:11.91]   Finished:    Microsoft.Maui.Controls.Xaml.UnitTests
  Passed ImplicitStyleBasedOnKeyedStyle(inflator: SourceGen) [531 ms]
  Passed ImplicitStyleBasedOnKeyedStyle(inflator: XamlC) [3 ms]
  Passed ImplicitStyleBasedOnKeyedStyle(inflator: Runtime) [51 ms]
Test Run Successful.
Total tests: 3
     Passed: 3
 Total time: 13.2524 Seconds

Without fix Maui24667Rd1

  Determining projects to restore...
  All projects are up-to-date for restore.
Test run for /home/vsts/work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net11.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll (.NETCoreApp,Version=v11.0)
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.8.2+699d445a1a (64-bit .NET 11.0.0)
[xUnit.net 00:00:00.69]   Discovering: Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:10.94]   Discovered:  Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:10.97]   Starting:    Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:11.05]   Finished:    Microsoft.Maui.Controls.Xaml.UnitTests
No test matches the given testcase filter `Maui24667Rd1` in /home/vsts/work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net11.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll

With fix Maui24667Rd1

  Determining projects to restore...
  All projects are up-to-date for restore.
Test run for /home/vsts/work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net11.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll (.NETCoreApp,Version=v11.0)
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.01] xUnit.net VSTest Adapter v2.8.2+699d445a1a (64-bit .NET 11.0.0)
[xUnit.net 00:00:00.42]   Discovering: Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:07.65]   Discovered:  Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:07.66]   Starting:    Microsoft.Maui.Controls.Xaml.UnitTests
[xUnit.net 00:00:07.69]   Finished:    Microsoft.Maui.Controls.Xaml.UnitTests
No test matches the given testcase filter `Maui24667Rd1` in /home/vsts/work/1/s/artifacts/bin/Controls.Xaml.UnitTests/Debug/net11.0/Microsoft.Maui.Controls.Xaml.UnitTests.dll

PR head: abe47b72.

Without-fix baseline: c7e8239c.

@MauiBot MauiBot added s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) and removed s/agent-review-in-progress AI review is currently running for this PR labels Nov 9, 2026
@kubaflo
kubaflo changed the base branch from main to candidate/11.0.1xx-sr2 November 10, 2026 21:27
@kubaflo
kubaflo merged commit 3c00b5f into dotnet:candidate/11.0.1xx-sr2 Nov 10, 2026
156 of 184 checks passed
kubaflo pushed a commit that referenced this pull request Nov 11, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 13, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 16, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 16, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 17, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 18, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 18, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 18, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 19, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 22, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
kubaflo pushed a commit that referenced this pull request Nov 23, 2026
…aries (#40103)

### Description of Change

When a `ResourceDictionary` is inflated at runtime (for example a style
dictionary without code-behind loaded through `Source=`), a
`BasedOn="{StaticResource …}"` lookup can only see resources already
added in that same file, plus application resources that already exist.
A base style living in a sibling merged dictionary is not reachable yet,
so loading the dictionary throws `XamlParseException: StaticResource not
found for key ButtonBaseStyle` and crashes the app at startup. Declaring
a global (implicit) style based on a shared keyed style this way is the
scenario from the issue.

**Fix**

In `StaticResourceExtension.ProvideValue`, when the unresolved target is
`Style.BasedOn` and the XAML root being parsed is a `ResourceDictionary`
(one that is still being loaded), defer the lookup to apply time through
the existing `Style.BaseResourceKey` mechanism instead of throwing. The
base style is then resolved through the consuming element's resource
scope, where the merged dictionaries are available. Failed lookups
anywhere else keep throwing exactly as before.

**Tests**

`Maui24667` (Xaml.UnitTests) inflates a page whose resources merge two
source dictionaries — a keyed `ButtonBaseStyle` and an implicit `Button`
style based on it — plus the issue's implicit-`BaseResourceKey` snippet,
and verifies the base setters are applied. The test runs on all three
inflators (Runtime, XamlC, SourceGen).

| | Without fix | With fix |
|---|---|---|
| Implicit style with `BasedOn="{StaticResource …}"` to a keyed style in
a sibling merged dictionary | `XamlParseException`: StaticResource not
found for key ButtonBaseStyle | Style applied, base setters inherited |
| Implicit style with `BaseResourceKey` (issue snippet) | works | works
|
| Full Xaml.UnitTests + Core.UnitTests | — | No new failures |

### Issues Fixed

Fixes #24667
@kubaflo kubaflo added this to the .NET 11 SR2 milestone Nov 24, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — abe47b72 Deployed Nov 9, 2026 by MykhailoDav via conclusion #2850
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash when declaring a global style with BasedOn or BaseResourceKey

3 participants