gpt4 book ai didi

c# - 为什么 MVVMLight 的 RelayCommand 使用 Wea​​kAction

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:29 24 4
gpt4 key购买 nike

我注意到 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 使用 Wea​​kAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51049392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com