TabItemClosingEvent and Async
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:
- User clicks on Close on MetroTabItem.
- I want to query whether user really wants to close the Metro Tabitem.
- 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.
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.
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.
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.
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 :)