开发者

Difference between custom and default closing event

开发者 https://www.devze.com 2023-03-24 13:43 出处:网络
I have a WPF Control (e.g. Button) that closes the applica开发者_JAVA技巧tion. I wonder are there any differences between a custom built event handler to close the application and the default closing

I have a WPF Control (e.g. Button) that closes the applica开发者_JAVA技巧tion. I wonder are there any differences between a custom built event handler to close the application and the default closing event:

Basically are there difference between the following two?:

private void Btn_Click(object sender, RoutedEventArgs e)
{
     RibbonWindow.Close();
}

and

protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);
}


Difference between custom event handler (Btn_Click) for closing app and default event (OnClosed)is that even if you close the app with custom event default will always be called at the end. That is not the case with default. Once default is called custom won't be called after that because obviously button is not clicked. In short, default event should be handled when you must need an event when application is closing (even after close button is clicked) while you can use custom event to ask user are you sure? kind of thing

For example let's say you create a temp file during application lifetime and you must have to delete before app exists.

Now you can put this code inside custom button click event but what if user closes the app with Alt + F4 or by any way other than your button? In that case your button code will not be called but default close event will always be called and you can safely delete the temp file.


The first example you provided is event handler of an event raised bu Button, I presume . This event raised when User clicks button. Second, instead, is in Override of an event raised by WPF system. This is completely different story. The event "source" changes.

Regards.

0

精彩评论

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