I noticed that GalaSoft.MvvmLight.CommandWpf.RelayCommand<T>
using WeakAction<T>
.
this design consideration was surprising to me and I did a little further investigation.
while browsing the web, I came across this blog post which strengthen my suspicion, but did not answer my original question as to "Why?"
Why would GalaSoft intentionally use WeakAction
instead of plain Action
allow the functions to get Garbage Collected?
Thank you.
Why would GalaSoft intentionally use
WeakAction
instead of plainAction
allow the functions to get Garbage Collected?
Most probably to prevent the commands(s) from keeping the owner(s) of the action(s) alive and cause memory leaks in an MvvmLight
application.
Using a WeakAction<T>
, the owner is still eligible for garbage collection despite the fact the command may be not as there is no strong reference between the command and the owner of the action.
This to create Weak Reference with the Action
's owner. As that the owner Action
can be garbage collected at any time.
Ref.
http://www.mvvmlight.net/help/SL5/html/aac8fdf1-873d-4e6b-ce31-af2dcf02d1d0.htm https://github.com/lbugnion/mvvmlight/blob/master/GalaSoft.MvvmLight/GalaSoft.MvvmLight%20(PCL)/Helpers/WeakAction.cs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.