My Silverlight 4 application keeps in contact with a server side through a wcf service. Whenever the user refreshes, navigates away or terminates the browser I should do some cleanup towards the server side.
I can not use the Application Exit event; my wcf client is dead before it eventually gets called. I can not use the (new in SL4) FrameworkElement Unloaded event; it ain’t called when the Silverlight app shuts down.
So, how do I detect the browser refresh, newpage or shutdown in time to do my cleanup?
BaBu,
I do this exact thing when a user navigates away from my Silverlight app (or does a refresh). Follow the steps below to catch this event.
1.) Start by listening for the HTML page’s “onbeforeunload” event, like so…
2.) Implement Application_BeforeExit() to setup and call an ASP.NET “PageMethod”, like so…
3.) Add the PageMethod to your page code behind (Index.aspx.cs), like so,
4.) Allow PageMethods on your page (Indx.aspx), like so,
Good luck,
Jim McCurdy, YinYangMoney.com