MahApps.Metro icon indicating copy to clipboard operation
MahApps.Metro copied to clipboard

TabItemClosingEvent and Async

Open saterbear opened this issue 12 years ago • 4 comments

This is my first post to the MahApps site here. I am really enjoying what I am seeing and what I have been able to do, but I can not figure out how to do the following:

  1. User clicks on Close on MetroTabItem.
  2. I want to query whether user really wants to close the Metro Tabitem.
  3. Depending on answer Yes/No, set the e.Cancel on the CancelEventArgs to prevent/allow the tabitem from closing and removed from the MetroTabControl.

I am using the ShowMessageAsync method asynchronously for querying the user. The query works fine, but the tabitem is removed regardless - I am assuming that is because of the async method. Disclosure - I have not used many async methods before so I am learning. I am using .NET 4.5.

I could go into detail of a lot of things I have tried, but you'd probably laugh. Any best practice here?

Thanks a lot.

saterbear avatar Sep 17 '14 22:09 saterbear

That's how async is implemented in C#. You have to set e.Cancel to true before making the async call. See #1523 for reference.

thoemmi avatar Sep 18 '14 07:09 thoemmi

Thanks for the reply. So, I understand that setting e.Cancel = true during the async call will prevent the tab from closing, but after the user responds "yes" to close the tab how do I once again close the tab since the TabItemClosingEvent is already done? The example in #1523 is good, but it handles the affirmative user input by just stopping the application using Application.Current.Shutdown(). I hope this makes sense. Thanks.

saterbear avatar Sep 20 '14 15:09 saterbear

So, I understand that setting e.Cancel = true during the async call will prevent the tab from closing, but after the user responds "yes" to close the tab how do I once again close the tab since the TabItemClosingEvent is already done?

Exactly. When the user confirms to close the tab, close it programmatically.

thoemmi avatar Sep 20 '14 21:09 thoemmi

Is your problem still unsolved?

Are you using the 'await' keyword for this? Your code execution won't 'pause' if you don't use that keyword.

Other way: can you post the function that code is in? I won't laugh: I learned a few async/await tricks because of this project :)

Dotnetifier avatar Oct 02 '14 17:10 Dotnetifier