Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lack of notification for x:Load #10405

Open
sjb-sjb opened this issue Mar 6, 2025 · 3 comments
Open

Lack of notification for x:Load #10405

sjb-sjb opened this issue Mar 6, 2025 · 3 comments
Labels
bug Something isn't working needs-triage Issue needs to be triaged by the area owners

Comments

@sjb-sjb
Copy link

sjb-sjb commented Mar 6, 2025

Describe the bug

The page on x:Load says that when the deferred element is loaded, “If you have registered to receive property change notifications on the property containing the deferred element(s), the notification is raised.”

But it also says that “The field for x:Name is set.” And indeed, the XAML element that has x:Load and x:Name is declared as a field with the specified name, not as a dependency property. So, actually there is no "property containing the deferred element(s)". And obviously you cannot subscribed to the Loaded event of an element whose field reference is null.

In other words, as far as I can tell there is actually no way to receive a change notification when a deferred element using x:Load is loaded. This breaks various types of code where an action must be taken when an element is loaded. For example, triggering a piece of code-behind to examine a property of the deferred element is not really possible since no notification is raised when the element is loaded.

It is not always possible to detect the condition that triggered the loading. For example, when the control containing the x:Load element is a parameter to a library method. As well, if x:Load is bound to a path and one attempts to trigger a notification of the load using a separate binding based on the same path, then there is a race condition between the two bindings.

Steps to reproduce the bug

Create a XAML user control and specify x:Load on one of the elements in the user control. Then, try to get a notification of the element being loaded. No can do!

Expected behavior

There should be a mechanism for raising a notification when x:Load is triggered to load the element.

Screenshots

No response

NuGet package version

None

Windows version

No response

Additional context

No response

@sjb-sjb sjb-sjb added the bug Something isn't working label Mar 6, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Mar 6, 2025
@JohnnyWestlake
Copy link

JohnnyWestlake commented Mar 6, 2025

This may be a a misunderstand of what it is trying to say. This is an example of what the docs are saying:

  • You have a Border, which has a Rectangle set to Border.Child. The Rectangle is set to x:Load="False".
  • You register a DependencyPropertyChanged callback on Border.Child.
  • When you set x:Load to true on the Rectangle, the Border.Child DependencyPropertyChanged callback will be called.

In your case, specifying the Loaded event on the Rectangle in XAML will properly fire the Loaded/Unloaded events when it's added or removed from the VisualTree using x:Load

@sjb-sjb
Copy link
Author

sjb-sjb commented Mar 6, 2025

Hmmmm, thanks for this! What happens if the x:Load’d element is in, say, a Grid? This is often the case. I notice that Grid.Children is a UIElementCollection, which does not implement CollectionChanged. Again, it would seem that if I name the x:Load element and x:Bind to it or use a Binding to it then the binding will not be updated.

In other words, the comment about Border.Child being notified is a partial answer. It does not address how to get a notification of x:Load occurring for elements contained in a panel. This also does not provide a way to discover, say from code-behind, an element that can be listened to for the notification.

The added comment about specifying Loaded event on the x:Load element (the Rectangle) in XAML is interesting. I haven't tried this yet. This must be something specific to the XAML interpretation of event handlers, because in code-behind obviously I can't subscribe to the Rectangle's Loaded event when I don't have a reference to the Rectangle (which, presumably, has not even been created when the containing control is created).

My use case is listening for property changes in a c# library, rather than customized XAML code.

@JohnnyWestlake
Copy link

The added comment about specifying Loaded event on the x:Load element (the Rectangle) in XAML is interesting. I haven't tried this yet. This must be something specific to the XAML interpretation of event handlers, because in code-behind obviously I can't subscribe to the Rectangle's Loaded event when I don't have a reference to the Rectangle (which, presumably, has not even been created when the containing control is created).

x:Bind generated code will manually hook up XAML events when realising an object for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage Issue needs to be triaged by the area owners
Projects
None yet
Development

No branches or pull requests

2 participants