1
Vote

WeakAction issue for WinRT version

description

The implementation of WeakAction for WinRT doesn't work, since the target‘s hard reference is kept by the member Action object itself. as follow code:
        public WeakAction(object target, Action action)
        {
            if (target != null)
            {
                _reference = new WeakReference(target);
            }

            _action  = action;  // The hard reference of target still exsits.
        }
This issue has been spread to RelayCommand that uses the WeakAction for implementation.
It's easy to reproduce this issue. In winrt app, you can make a cycle reference though a lambda with 'this' as target and use WeakAction to replace with Action. you will find it really doesn't work.
I didn't test the WeakFunc class, but according the same implementation, it maybe have the same issue.

comments

lbugnion wrote Jun 25, 2013 at 4:17 PM

That is correct and is historical. The addition of the classes and properties needed to make this a real weak action (and Func) were added to Windows 8 only late, and I didn't have time to update the code yet. This is a high priority item on my list.

Note that even with these new additions, the restrictions will be the same as in Silverlight, meaning that only public methods will be truly "weak". There is just no other way to rehydrate a MethodInfo if it is private or internal, for security reasons :(

Laurent

congrats wrote Jun 26, 2013 at 3:00 AM

Thanks for your kindly reply, and sorry for my poor English.
Right on, I have seen your silverlight's implementation, hepefully it's would be done for winrt.

Whatever, very appreciate your great work in this project, actually, we use it a lot, and it really bring much convenience in my job.

Best Wishes
Haibo L.

onovotny wrote Jul 17, 2013 at 7:05 PM

Hi Haibo,

You can also use the PCL version of MVVM Light as I've implemented the fix there already. It's compatible with all of the current .NET versions.

On NuGet, install Portable.MvvmLightLibs instead of the current one. Remove the current version first to prevent Nuget issues...

Oren