c# - How to obtain x:Load notification - Stack Overflow

admin2025-05-02  0

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 when I look in the generated code, it’s true that the XAML element that has x:Load and x:Name is declared as a field with the specified name, not a dependency property. Now, granted, I am actually using winui rather than uwp. Still…

What I don’t understand is how I can be notified of the change to the field value, or any other way to be notified of the load and unload.

In other words, say I have a user control in WinUI that contains a control with x:Load, for example,

<ToggleSwitch
    x:Load={x:Bind ToggleSwitchIsNeeded, Mode=OneWay}
    >
</ToggleSwitch>

and I create the boolean dependency property named ToggleSwitchIsNeeded in the user control. How do I receive a notification in the user control when the ToggleSwitch is loaded or unloaded?

I can’t register for dependency property notifications on the name since the name is declared as a field rather than as a dependency property. I can’t register for the load event (at least not in code-behind, which is what I need to do) because I don’t have a reference to the object that is being loaded.

I don’t think that I can load the object explicitly in order to subscribe to its loaded/unloaded events because I am binding to x:Load so there would be a conflict between the x:Load binding and the explicit load — in order to prevent memory leaks I would need to unsubscribe in unloaded, which would defeat the purpose.

Thoughts on this? How do I set up a notification in code-behind of the load and unload or of a property change?

MS Learn refs: @mattwojo or @jwmsft

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 when I look in the generated code, it’s true that the XAML element that has x:Load and x:Name is declared as a field with the specified name, not a dependency property. Now, granted, I am actually using winui rather than uwp. Still…

What I don’t understand is how I can be notified of the change to the field value, or any other way to be notified of the load and unload.

In other words, say I have a user control in WinUI that contains a control with x:Load, for example,

<ToggleSwitch
    x:Load={x:Bind ToggleSwitchIsNeeded, Mode=OneWay}
    >
</ToggleSwitch>

and I create the boolean dependency property named ToggleSwitchIsNeeded in the user control. How do I receive a notification in the user control when the ToggleSwitch is loaded or unloaded?

I can’t register for dependency property notifications on the name since the name is declared as a field rather than as a dependency property. I can’t register for the load event (at least not in code-behind, which is what I need to do) because I don’t have a reference to the object that is being loaded.

I don’t think that I can load the object explicitly in order to subscribe to its loaded/unloaded events because I am binding to x:Load so there would be a conflict between the x:Load binding and the explicit load — in order to prevent memory leaks I would need to unsubscribe in unloaded, which would defeat the purpose.

Thoughts on this? How do I set up a notification in code-behind of the load and unload or of a property change?

MS Learn refs: @mattwojo or @jwmsft

Share Improve this question edited Jan 4 at 12:47 sjb-sjb asked Jan 2 at 4:15 sjb-sjbsjb-sjb 1,2258 silver badges17 bronze badges 2
  • It might be easier to help if you could provide specific code with your issue. – Andrew KeepCoding Commented Jan 3 at 3:45
  • Hi @AndrewKeepCoding, sure, I have edited to include an example. – sjb-sjb Commented Jan 4 at 12:50
Add a comment  | 

2 Answers 2

Reset to default 0

It cannot get notifications or property change from the code-behind, because you cannot get the deferred elements from the visual tree, when an object is unloaded, it will be replaced in the tree with a placeholder.

There are some known issues around Loaded/Unloaded events. I guess you already know this.

As a workaround for this case, you can monitor the ToggleSwitchIsNeeded bound to x:Load:

public static readonly DependencyProperty ToggleSwitchIsNeededProperty =
    DependencyProperty.Register(
        nameof(ToggleSwitchIsNeeded),
        typeof(bool),
        typeof(UserControl1),
        new PropertyMetadata(default, OnToggleSwitchIsNeededPropertyChanged));

private static void OnToggleSwitchIsNeededPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    System.Diagnostics.Debug.WriteLine($"OnToggleSwitchIsNeededPropertyChanged: {e.OldValue} -> {e.NewValue}");
}

public bool ToggleSwitchIsNeeded
{
    get => (bool)GetValue(ToggleSwitchIsNeededProperty);
    set => SetValue(ToggleSwitchIsNeededProperty, value);
}
转载请注明原文地址:http://anycun.com/QandA/1746134719a92054.html