-
Notifications
You must be signed in to change notification settings - Fork 693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Add Application.OnUnload or OnExit #9165
Comments
There is something that you are able to do now. This simulates the behaviour that you want. But it is okay to treat this as a work around. It requires overriding the Xaml generated main, but it is pretty quick and easy to do. I'll give instructions for both C++ and C# here. First, override wWinMain/Main. You can find the generated version in a generated file named App.xaml.g.hpp or App.g.i.cs. You can find the entry point function here surrounded by the DISABLE_XAML_GENERATED_MAIN preprocessor directive.
Not only does this allow you to disable the generated entry point, it also gives you a template to work with. Add DISABLE_XAML_GENERATED_MAIN as a C++ preprocessor definition or a C# conditional compilation symbol.
I tidied the code up a little. You can build and run the application to make sure that it works. Second. Add some cleanup functionality to App.
Cleanup will be used to remove all resources used by App. In this example, I just use it to remove the reference to the application's window. Finally, we take advantage of the fact that the callback passed to Start is a lambda. We add a variable to wWinMain/Main to hold our App reference, and then use the lambda capture to assign to the App instance.
There is a little quirkiness with the C++ version due to the projection, but that is it. |
Thank you for that !!! It does fullfill my needs!! Greatly appreaciated 👍 A real implementation of THAT solution inside the Application class would be nice to have eventually. |
Why not use a global object with destructor? |
Another possible workaround:
|
Proposal: Add virtual method for application exit in WINUI (C#)
Summary
Be able to hook on application exit just like the existing OnLaunched
Rationale
I'm using an existing code "C" code base using Interop (P/Invoke) calls from our existing system. I need to initialise the "library" on launch, but also need to "shutdown" that library properly when the application exits.
Right now, we're stuck to unload the library using the window close operation. But since we have multiple top level window, we need some weird code to make sure we perform the exit procedure on the "last window to close".
Would be nice to simply do that using an OnExit override on the Application class.
Note
Using WinUI in unpackaged C# application.
The text was updated successfully, but these errors were encountered: