何时实际调用 IAttachedObject.Detach() (KeyTrigger.Detaching()) 方法?
假设我有一个带有文本框的 Silverlight 用户控件:
<TextBox>
<ii:KeyTrigger Key="Enter">
<!-- something here -->
</ii:KeyTrigger>
</TextBox>
我将用户控件注入到根内容控件中:
root.Content = new MyUserControl();
现在我知道当 MyUserControl 的“Loaded”事件发生时,KeyTrigger 将连接到 TextBox:
AssociatedObject.KeyDown+=SomethingHere;
其中 AssociatedObject 将是其中的 TextBox案件。
该事件在 KeyTrigger 的 OnDetaching 方法重载中被取消订阅,问题是我不知道 OnDetaching 何时被调用......任何人都可以对此有所了解吗?
我有一个按钮,将用户控件的新实例注入根内容:
root.Content = new MyUserControl();
每次发生这种情况时,新实例上都会发生“Loaded”事件,并且调用 KeyTrigger 的事件,但不会为“旧”用户控件实例调用 OnDetaching...这不会导致内存泄漏吗? 当控件从可视树中消失时,是否会调用 OnDetaching (Detach)?
Let's say I have a Silverlight user control with a textbox:
<TextBox>
<ii:KeyTrigger Key="Enter">
<!-- something here -->
</ii:KeyTrigger>
</TextBox>
I inject my user control into root content control:
root.Content = new MyUserControl();
Now I know that KeyTrigger will be hooked up to TextBox when "Loaded" event of MyUserControl occurs:
AssociatedObject.KeyDown+=SomethingHere;
Where AssociatedObject will be a TextBox in this case.
The event is unsubscribed in OnDetaching method overload of KeyTrigger, the problem is that i dont know when OnDetaching is being called... Anyone could shed some light on this?
I have a button that injects a new instance of user control into root content:
root.Content = new MyUserControl();
everytime this happens "Loaded" event occurs on new instance and event for KeyTrigger is called, no OnDetaching is called for "old" user control instance however... won't this cause memory leaks?
Is OnDetaching (Detach) called when a control dissapears from visual tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要处理 FrameworkElement.Closed 事件 - 仅当触发器从其 DependencyObject 中显式删除时才会调用 OnDetached。
You want to handle the FrameworkElement.Closed event - OnDetached only gets called when the trigger is explicitly removed from its DependencyObject.