MvvmLight message not firing when using local/anon

2019-07-03 22:52发布

问题:

MvvmLight messenger sometimes doesn't work when I use an anonymous action. If I pass a member variable or method as the action it works fine, but using an anonymous lambda or local variable doesn't work.

    private SongCollection songCollection;
    Action<bool> c;
    public MyService(SongCollection songCollection)
    {
        this.songCollection = songCollection;

        Action<bool> a = (bool isLoading) =>
        {
            ChangeSong(songCollection.GetFirstSong());
        }; 

        Action<bool> b = OnLoadingComplete; //Using this instead of 'a' works.
        //c = a; //Uncommenting this line makes it work, even if using 'a'.

        Messenger.Default.Register<bool>(this, "IsLoading", a); //Doesn't work.
    }

I'm guessing it's something to do with garbage collection or the way MvvmLight does works behind the scenes. Or am I missing something obvious?

I'm using version MvvmLight 4.3.31.1 on .Net4.0.

标签: c# mvvm-light