Event fires more and more times

前端 未结 4 2068
余生分开走
余生分开走 2020-12-06 18:40

I have a silverlight mvvm application that loads main view with 2 user controls loaded into 2 ContentControls, one with listbox showing items and other with edit button. Whe

4条回答
  •  独厮守ぢ
    2020-12-06 19:15

    this.globalEvents.OnSaveButtonClicked += (s, e) => SaveData();
    

    This line is being called multiple times so you are adding a new event handler every time.

    You need to either move that line to somewhere where it's only called once or change the event handler to:

    this.globalEvents.OnSaveButtonClicked += SaveData;
    
    public void SaveData(object sender, EventArgs e)  
    {  
        globalEvents.RaiseSaveData(EditedGuy);     
        this.globalEvents.OnSaveButtonClicked -= SaveData();
    }
    

    So you remove the event handler after dealing with it. This assumes that the handler will be added back next time you go into edit mode.

提交回复
热议问题