![](/img/trans.png)
[英]System.Windows.Interactivity.EventTrigger in codebehind works on some Controls, but not others
[英]Events in ViewModel with System.Windows.Interactivity.EventTrigger, is it weak referencing?
我在ViewModel中有一個事件。
public class MyViewModel:ViewModelBase
{
...
public event EventHandler SomethingChanged;
private void FireEvent()
{
if (SomethingChanged != null)
SomethingChanged(this, EventArgs.Empty);
}
...
}
在我的視圖中,我使用EventTrigger來監聽事件以調用一個動作。
<i:Interaction.Triggers>
<i:EventTrigger EventName="SomethingChanged" SourceObject="{Binding}">
<i:SomeAction/>
</i:EventTrigger>
</i:Interaction.Triggers>
它工作得很好。 但是我很好奇是否存在內存泄漏的可能性? 由於我的View是按需創建的,因此意味着根據用戶在Visual樹中添加和刪除控件。 源對象(ViewModel)的生命周期比偵聽器(View)長。 因此,使用上面的代碼,事件是否仍然強烈引用已刪除的偵聽器(View)?
在EventTriggerBase的OnDetaching中,它調用以下代碼:
this.OnSourceChanged(this.Source, null);
這意味着源從Source設置為null並且事件已取消訂閱。 正確卸載視圖時沒有內存泄漏,這意味着正在調用Detach方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.