开发者

How to remove handler at control

I have a开发者_开发知识库 control which has in a constructor

_myWebService.ForwardMessageCompleted += new EventHandler(OnForwardMessageCompleted);

private void OnForwardMessageCompleted(object sender, AsyncCompletedEventArgs e)
        {
            activity.IsActive = false;
            if (e.Error == null)
            {
                RadWindow.Alert("The message has been forwarded");
            }
        }

Control has list of messages, and user can forward its to other users. The problem is, that user opens this control many times, and each time which it is created handler is being added to

_myWebService.ForwardMessageCompleted

How can I remove handling to this event?


Because you are connecting an external event handler in the constructor of a control, that event listener is also acting as a pointer to the control (so it is not being destroyed and the code gets called for every instance of the control).

You want to catch the Unloaded event of your control and add the following to that handler (to disconnect the event listener):

_myWebService.ForwardMessageCompleted -= OnForwardMessageCompleted;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜