wpf 不调用行为的 OnDetaching 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30638292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnDetaching function of behavior is not called
提问by Hodaya Shalom
I have WPF behavioron specific control. When I close the window that hold the control the OnDetachingfunction is not called.
我WPF behavior对具体的控制。当我关闭持有控件的窗口时,OnDetaching不会调用该函数。
The behavior continues to exist (because of the events to which it's registered), although the window does not exist anymore (memory leak).
该行为继续存在(因为它注册到的事件),尽管窗口不再存在(内存泄漏)。
Why does the OnDetachingfunction not fire, and how can I solve it?
为什么该OnDetaching功能没有触发,我该如何解决?
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.MouseLeftButtonDown += AssociatedObject_PlotAreaMouseLeftButtonDown;
this.AssociatedObject.MouseLeftButtonUp += AssociatedObject_PlotAreaMouseLeftButtonUp;
this.AssociatedObject.MouseMove += AssociatedObject_PlotAreaMouseMove;
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.MouseLeftButtonDown -= AssociatedObject_PlotAreaMouseLeftButtonDown;
this.AssociatedObject.MouseLeftButtonUp -= AssociatedObject_PlotAreaMouseLeftButtonUp;
this.AssociatedObject.MouseMove -= AssociatedObject_PlotAreaMouseMove;
}
采纳答案by Noam M
The OnAttachedis called when XAML parser parses XAML and creates an instance of a behaviour that adds to the BehaviorCollection of the target control which is exposed as DependencyAttached property.
在OnAttached当XAML解析器解析XAML并创建添加到暴露作为DependencyAttached属性目标控制的BehaviorCollection一个行为的一个实例被调用。
However when if the view is disposed, the collection (Behavior collection) was disposed of, it will never trigger OnDetaching method.
但是,如果视图被释放,则集合(行为集合)被释放时,它永远不会触发 OnDetaching 方法。
If the behaviour is not properly cleanup it will not be collected by GC and will also hold BehaviorCollection and other behaviors in that collection. The behaviours are designed to extend AssociatedObject, as long as you are subscribing to AssociatedObject events its fine as the AssociatedObject (publisher) will die and your behaviour will be collected by garbage collector.
如果行为没有被正确清除,GC 将不会收集它,并且还会在该集合中保存 BehaviorCollection 和其他行为。这些行为旨在扩展 AssociatedObject,只要您订阅 AssociatedObject 事件就可以了,因为 AssociatedObject(发布者)将死亡并且您的行为将被垃圾收集器收集。
An alternative is to handle the "Closing" event (when a user clicks the upper right 'X' button) of the window, and OnDetachingthere.
另一种方法是处理窗口的“关闭”事件(当用户单击右上角的“X”按钮时),然后OnDetaching在那里。
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<cmd:EventToCommand Command="{Binding CloseCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Then associate the handler in the View constructor:
然后在 View 构造函数中关联处理程序:
MyWindow()
{
// Set up ViewModel, assign to DataContext etc.
Closing += viewModel.OnWindowClosing;
}
And add the handler to the ViewModel:
并将处理程序添加到 ViewModel:
public void OnWindowClosing(object sender, CancelEventArgs e)
{
// Cancel, OnDetaching, etc
}
回答by Omri Aviv
Try to subscribe to AssociatedObject.Unloaded event and inside the eventHander unsubscribe to all the mouse events. Behaviors OnDetaching() func. not always invoked at "time".
尝试订阅 AssociatedObject.Unloaded 事件并在 eventHander 内取消订阅所有鼠标事件。行为 OnDetaching() 函数。并不总是在“时间”调用。

