c# - 为什么MVVMLight的RelayCommand使用WeakAction

标签 c# wpf mvvm garbage-collection mvvm-light

我注意到GalaSoft.MvvmLight.CommandWpf.RelayCommand<T>使用WeakAction<T> .

这种设计考虑让我感到惊讶,我做了一些进一步的调查。
在浏览网页时,我遇到了this blog post这加强了我的怀疑,但没有回答我最初的问题“为什么?”

为什么 GalaSoft 会故意使用 WeakAction而不是简单的 Action允许函数收集垃圾吗?

谢谢。

最佳答案

Why would GalaSoft intentionally use WeakAction instead of plain Action allow the functions to get Garbage Collected?

很可能是为了防止命令使操作的所有者保持事件状态并导致 MvvmLight 中的内存泄漏应用程序。

使用 WeakAction<T> ,尽管命令可能不符合垃圾回收的条件,但所有者仍然有资格进行垃圾回收,因为命令和操作的所有者之间没有强引用。

关于c# - 为什么MVVMLight的RelayCommand使用WeakAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51049392/

相关文章:

c# - Paint.NET 风格的工具窗口

c# - 使用属性定义 UI 的属性

android - Java Android MVVM-如何将存储库中的变量发送回 Activity ?

wpf - 如何将复选框与其内容对齐?

c# - 如何在 WPF 工具包 Datagrid 的 ContextMenu CommandParameter 中设置绑定(bind)

wpf - 如何从 Brush(例如 DrawingBrush)转换为 BitmapSource?

mvvm - 如何将 View 模型绑定(bind)到 MVVMCROSS 中的 UserControl?

c# - Facebook C# SDK 和访问 token

c# - 无法访问内部接口(interface) "IWindowServices"

c# - 更新现有行 mysql linq