我在 WPF 应用程序中使用 MVVM 模式,出于教育目的,我有一个关于内存泄漏的问题。
我一直在使用 Postsharp 库。
我的 ViewModel 如下所示:
public class MyViewModel : INotifyPropertyChanged
{
private ClassA _myClassAField; //ClassA doesn't Implement INotifyPropertyChanged
public MyViewModel(ClassA classAParameter)
{
_myClassAField = classAParameter;
}
[NotifyPropertyChanged]
public string Name { get { return _myClassAField.Name; } }
.....//Other Code.....
}
Xaml:
<Textblock Text="{Binding Name, Mode=OneWay}"/>
问题:
此绑定(bind)是否会泄漏内存? 在我的测试中似乎没有泄漏,但我不明白为什么?
与属性名称的绑定(bind)只是来自 ClassA 的 getter,不实现 INotifyPropertyChanged。
最佳答案
您的 ClassA
未实现 INotifyPropertyChanged
,但您绑定(bind)到类 MyViewModel
中的属性,而该类确实实现了!因此你不应该出现内存泄漏。
There is an issue where WPF checks to find things that implement INotifyProperyChanged. If there is a databinding to something not implementing this interface, then it makes a record in a global table. That record doesn’t get cleaned up, as WPF has no way of checking when that DB record is no longer needed.
关于c# - 这个单向绑定(bind)会泄漏内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37393198/