MvvmLight message not firing when using local/anonymous action

感情迁移 提交于 2019-12-23 21:43:53

问题


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.

来源:https://stackoverflow.com/questions/23576000/mvvmlight-message-not-firing-when-using-local-anonymous-action

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!