Skip to content

XAML attached-property binding paths read null once a second ALC has loaded the owner assembly #24581

Open

Description

@Xiaoy312

Current behavior

A template binding whose path is a parenthesized attached property reads null once more than one AssemblyLoadContext has loaded the assembly that declares the attached property:

Content="{Binding Path=(ut:ControlExtensions.Icon), RelativeSource={RelativeSource TemplatedParent}}"

with xmlns:ut="using:Uno.Themes".

The page has set ControlExtensions.Icon on that very element, the style applies, and the type-keyed DP lookup returns the correct per-ALC DependencyProperty — only the value the binding path reads is null.

Nothing logs: a binding that resolves to null is not an error, and in this case the sibling Visibility binding through NullToCollapsedConverter then legitimately collapses the icon slot.

Measured on the hosted page (IconPresenter is the template part fed by the binding):

Material guest loaded first Material guest loaded after Cupertino + Simple
ControlExtensions.Icon on the TextBox SymbolIcon SymbolIcon (set correctly)
MaterialFilledTextBoxStyle applied yes yes
DependencyProperty.GetProperty(typeof(ControlExtensions), "Icon") matches the static DP matches the static DP
Uno.Themes.WinUI copies loaded 1 3
IconPresenter.Content SymbolIcon, 30x20 null, 0x0

The host's two existing workarounds do not help, and would not be expected to: its reflective clear of DependencyProperty._getPropertyCache (the #24073 mitigation) runs and logs no failure, and its Application.CleanupNonDefaultAlcCaches call (the #24075 mitigation) is confirmed to be invoked — its Uno 7 arity change had silently disabled it for a while, and that was repaired before these measurements. Nothing cached is stale here; the resolution is wrong every time it runs.

Expected behavior

An attached-property binding path resolves its owner type against the ALC of the element being bound (or of the parse context that produced the template), so each guest reads the DependencyProperty instance registered by its own copy of the declaring assembly.

Repro

Host three apps as secondary-ALC guests through AlcContentHost / WindowHelper.ContentHostOverride, with Uno.UI shared with the default ALC and the app-side library declaring the attached property isolated per guest.

In Uno.Themes, ThemesSampleApp does exactly this — GuestSharedAssemblies.txt shares =Uno.UI and isolates !Uno.Themes.WinUI:

  1. Launch ThemesSampleApp (desktop, Debug), load the Cupertino or Simple guest first, then switch to Material.
  2. Navigate to Helpers → ControlExtensions.
  3. The TextBox and ComboBox leading icons are missing.

Load Material first in a fresh process and they render correctly — the defect needs a second copy of the declaring assembly to already be loaded.

Affected markup: v2/TextBox.xaml#L450 and v2/ComboBox.xaml#L601; the attached property is ControlExtensions.IconProperty.

Mechanism

Line references are against master @ 791095b; all the cited code predates the version observed on.

Confirmed by probe — see Measured resolution below.

The owner type is resolved by name, with no ALC scope active

DependencyProperty.InternalGetProperty discards the caller's type in favour of the one parsed out of the path string, so an attached-property path's owner type never benefits from the bound element's own ALC:

var propertyInfo = DependencyPropertyDescriptor.Parse(name);

if (propertyInfo != null)
{
    type = propertyInfo.OwnerType;   // by-name resolution wins over the element's own type
    name = propertyInfo.Name;
}

DependencyPropertyDescriptor.Parse resolves Uno.Themes.ControlExtensions via the metadata provider and then SearchTypeInLoadedAssemblies, which delegates to ContextualAssemblyResolver.GetRelevantAssemblies():

var contextual = AssemblyLoadContext.CurrentContextualReflectionContext;
if (contextual is not null && contextual != AssemblyLoadContext.Default)
{
    return contextual.Assemblies.Concat(AssemblyLoadContext.Default.Assemblies);
}

return AppDomain.CurrentDomain.GetAssemblies();

That resolver is ALC-aware only while a contextual-reflection scope is active, and its own summary says why the fallback is dangerous here:

This avoids picking up stale or sibling per-app ALCs that AppDomain.GetAssemblies would otherwise expose long after they have been Unloaded but not yet finalised.

No such scope is active when a template's bindings are resolved. The only EnterContextualReflection in all of Uno.UI is in PageStackEntry.ResolveDescriptor, wrapped narrowly around resolving a page type descriptor. Template application and binding evaluation happen later, during layout, outside it. So resolution falls through to AppDomain.CurrentDomain.GetAssemblies() and picks whichever Uno.Themes.WinUI that enumeration yields first — including copies belonging to guest ALCs that have already been unloaded but not yet collected, which is precisely the case the resolver was written to avoid.

That matches the symptom exactly: one copy loaded, the right one is picked; a second copy already present, the earlier one wins and the binding reads a DependencyProperty the page never wrote to.

Measured resolution

Run from the host, in the default ALC, after each guest load — replaying SearchTypeInLoadedAssemblies verbatim (Type.GetType, then the AppDomain.CurrentDomain.GetAssemblies() scan) and reporting the AssemblyLoadContext of the winning type. Desktop, Debug, 7.0.0-dev.701.

Type.GetType("Uno.Themes.ControlExtensions") returned null at every step, and AssemblyLoadContext.CurrentContextualReflectionContext was <none> at every step — so every resolution went through the AppDomain scan.

Catalog order (Material loaded first):

Hosted guest Uno.Themes.WinUI copies Owner type resolved to
Material 1 GuestALC-MaterialSampleApp ✅
Cupertino 2 GuestALC-MaterialSampleApp ❌
Simple 3 GuestALC-MaterialSampleApp ❌

Reverse order (Simple loaded first) — the reported repro's configuration:

Hosted guest Uno.Themes.WinUI copies Owner type resolved to
Simple 1 GuestALC-SimpleSampleApp ✅
Cupertino 2 GuestALC-SimpleSampleApp ❌
Material 3 GuestALC-SimpleSampleApp ❌

The winner is pinned to the first-loaded guest and never tracks the hosted one. Note also that the copy count only grows: each guest is unloaded before the next loads, yet its Uno.Themes.WinUI stays visible to AppDomain.GetAssemblies() — the stale-ALC exposure the resolver's own summary warns about.

Symmetry worth noting: Material is simply the most visible victim because its templates bind .Icon. Simple's templates bind ControlExtensions.LeadingIcon / .TrailingIcon, so a Simple guest loaded after another guest should lose its leading/trailing icons the same way; Cupertino references ControlExtensions nowhere and is unaffected.

The documented mitigation is unavailable here

ContextualAssemblyResolver's remarks prescribe the fix to consumers, and the example is this exact scenario:

// Bindings with cross-ALC attached-property paths:
using (alc.EnterContextualReflection())
{
    myElement.SetBinding("(local:MyAttached.Value)", new Binding { Path = new PropertyPath("Value") });
}

That works when the consumer calls SetBinding itself. A template-declared binding gives the consumer no call site to wrap — the framework creates it during template application. So for XAML-declared bindings the prescribed mitigation is structurally unreachable, which is why this is being reported rather than fixed host-side.

Two mechanisms ruled out

Recorded so they do not have to be re-tread. Both are consistent with the probe above, which shows the wrong owner type being selected before either could apply:

  • Not the by-name metadata-provider fast path. BindableMetadataProvider.GetBindableTypeByFullName is indeed a process-wide by-name lookup on the shared Uno.UI, and it is unguarded where the sibling GetBindableTypeByType got GetValidatedBindableType. But each guest installs its own generated provider at startup, so last-loaded-wins would make the Material provider current in the broken configuration — predicting it should work. The load order points the other way.
  • Not a shared getter memo. BindingPropertyHelper._getValueGetter is keyed by (Type, path string, bool) and would collide across guests on (typeof(TextBox), "(ut:ControlExtensions.Icon)", false). But no earlier guest ever installs that entry: Cupertino's styles reference ControlExtensions nowhere at all, and Simple's use only LeadingIcon / TrailingIcon — different path strings, different keys. Only Material binds .Icon.

How to fix

  • Enter the bound element's ALC contextual-reflection scope around template application / binding-path resolution, so GetRelevantAssemblies() takes its ALC-aware branch for framework-created bindings as it does for consumer-created ones.
  • Or resolve the owner type against the bound element's context directly — AssemblyLoadContext.GetLoadContext(element.GetType().Assembly) — instead of a process-wide scan, and stop discarding the caller's type in InternalGetProperty when the path names an owner that exists in the element's own context.

Host-side workaround: none in use. Now that the mechanism is known, a host could plausibly establish an EnterContextualReflection scope around guest UI work — but binding evaluation happens on the dispatcher during layout, not at a call site the host controls, so this was not attempted and is not known to work. The mitigation actually in use is to load the design system under test first in a session, or run its head standalone.

Environment

Observed against Uno.Sdk.Private 7.0.0-dev.701, Skia desktop on Windows, hosting the three Uno.Themes sample heads as secondary-ALC guests through AlcContentHost / WindowHelper.ContentHostOverride. Not re-tested against 6.7, so this is where it was observed rather than a bisected introduction point.

Found while working on the ThemesSampleApp wrapper head (see unoplatform/Uno.Themes#1693); this is the fifth issue from that effort, after #24073, #24074, #24075 and #24076.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions