-
Notifications
You must be signed in to change notification settings - Fork 678
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
unloaded event is triggered for the control when the control is set as content for the ContentDailog #8342
Comments
Hi Team, Any update regarding this? |
Hi @bpulliam , Any update regarding this bug? |
The issue that occurs in that the control gets added to the tree, removed and then re-added. We have identified an issue where the "Unloaded" event gets fired removal, but since it is an asynchronous event, the application doesn't get notified until the element is already back in the tree. Unfortunately, we have not found a way to correct this issue without potentially breaking applications that may have inadvertently relied upon this behavior (including some internal Xaml code). Because of this, we will try to target a fix for the 2.0 release, but until then you will need to work around the issue and fortunately there appears to be such a workaround. What is happening is that the Unloaded event is getting fired asynchronously when the control is removed from the tree. But then it gets re-added and, by the time the event is actually processed by the application, the element is back in tree. To get around this issue, the application can look at the IsLoaded property on the element and only remove the handler if the value is false. Since the element is, in fact, in the tree, another Unloaded event will come when it is removed a second time. Details: First a bit of a side as to why this TextBox is getting removed and re-added. When content dialog is shown, it ends up being added to the tree (causing anything in it to be added to the tree), but then after the smoke layer is created (which happens during template application), it gets removed and re-added to ensure the correct z-order between the dialog content and the smoke layer. So from the perspective of a control, it gets added-removed-added, causing this issue. Now to the events. Because of the nature of these tree related events, they are raised at different times and in different manners in order to assure that things get manipulated predictably (which they are, it just seems not correctly). For example, Loading and Loaded are raised synchronously since an application might want to modify the tree in them and want to limit the amount of stale UI we render. But Unloaded, is asynchronous since is primarily just resource cleanup and there is no need to delay a render frame for it. So basically, what happens is: When an element is added to the tree, is marked as needing a Loading event. But we want some "order" to how we raise these (other than the order an application might add them) so we do not raise it immediately.
The issue occurs if you add then remove, then re-add the element in the same tick. Here is what happens:
So now we have a unloaded event fired while the element is in the tree, thus causing this issue. The fix: Although we are still discussing options, we think the fix is that we would ensure that we don't raise an Unloaded event:
It is the first condition that we found breaks some internal code and could potentially break some applications. For now we are going to keep this issue open to track our work on hopefully getting a solution for 2.0. Two other issue that are caused by the same series of events: |
Thank you for the update. We will wait for the fix. |
Thanks for the detailed reply @JJBrychell, I appreciate the in depth explanation! For anyone who's searching, this bug causes public abstract class Behavior<T> : DependencyObject, IBehavior
where T : DependencyObject
{
protected Behavior()
{
AssociatedObject = null;
}
DependencyObject IBehavior.AssociatedObject => AssociatedObject;
public T AssociatedObject
{
get;
private set;
}
public void Attach( DependencyObject? associatedObject )
{
if( associatedObject == null || ReferenceEquals( associatedObject, AssociatedObject ) || DesignMode.DesignModeEnabled )
{
// do nothing, object is already attached
}
else if( associatedObject is T typedObject )
{
AssociatedObject = typedObject;
OnAttached();
}
else
{
throw new InvalidOperationException( $"AssociatedObject is expected to be type {typeof( T ).FullName} but was {associatedObject.GetType().FullName}" );
}
}
public void Detach()
{
if( AssociatedObject is FrameworkElement { IsLoaded: true } )
{
// This case happens when the control is removed from the visual tree and added back before the Unloaded event is fired.
// Details on why this happens can be found here: https://github.com/microsoft/microsoft-ui-xaml/issues/8342#issuecomment-2031017667
// This bug is expected to be fixed in the WindowsAppSdk 2.0 timeframe, at which point this workaround can be removed.
}
else
{
OnDetaching();
AssociatedObject = default!;
}
}
protected virtual void OnAttached()
{
}
protected virtual void OnDetaching()
{
}
} |
One other issue that has been pointed out based upon a duped bug is that if we add a collapsed element to the tree, we won't get the loading because we don't measure collapsed elements. But we do get the loaded event when we complete the next layout pass. Then when the element is made visible, we will get the loading event (even though it is already loaded) and don't get the loaded event (even though templates may have been expanded). |
Does the bug fixed in version 1.6.1? |
Describe the bug
The TextBox control is set as content for the contentDailog which is called to show inside the button click. When we call the content dailog through the button click, the unloaded event is called for the TextBox control. It should not called when the text box is display in the view.
Steps to reproduce the bug
SfCalendarDatePickerBug.zip
Expected Behavior
Expected behavior
It should trigger the loaded event only.
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.2.5: 1.2.230313.1
Windows version
Windows 11 (22H2): Build 22621
Additional context
No response
The text was updated successfully, but these errors were encountered: