How to detect a browser refresh from Silverlight 4?
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) FrameworkEleme开发者_如何学JAVAnt 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...
public void Application_Startup(object sender, StartupEventArgs e)
{
bool ok = HtmlPage.Window.AttachEvent("onbeforeunload", Application_BeforeExit);
ok = HtmlPage.Document.AttachEvent("onbeforeunload", Application_BeforeExit);
MainPage mainPage = new MainPage();
base.RootVisual = mainPage;
}
2.) Implement Application_BeforeExit() to setup and call an ASP.NET "PageMethod", like so...
private void Application_BeforeExit(object sender, HtmlEventArgs args)
{
string methodName = "ModelShutdown";
params object[] args = new Guid().ToString());;
try
{
ScriptObject pageMethods = (ScriptObject)HtmlPage.Window.GetProperty("PageMethods");
if (pageMethods == null)
throw new ArgumentException("Web page does not support PageMethods");
object[] pageMethodArgs = { new PageMethodEventHandler(Success), new PageMethodEventHandler(Failure), null/*userContext*/};
object[] combinedArgs = new object[args.Length + pageMethodArgs.Length];
args.CopyTo(combinedArgs, 0);
pageMethodArgs.CopyTo(combinedArgs, args.Length);
pageMethods.Invoke(methodName, combinedArgs);
}
catch (Exception ex)
{
//ex.Alert();
}
}
3.) Add the PageMethod to your page code behind (Index.aspx.cs), like so,
public partial class Index : Page
{
[WebMethod] // a PageMethod called from Silverlight
public static void ModelShutdown(string identifier)
{
System.Diagnostics.Debug.WriteLine("*** Signing Off: " + identifier);
}
}
4.) Allow PageMethods on your page (Indx.aspx), like so,
<asp:ScriptManager runat="server" EnablePageMethods="true" />
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
Good luck,
Jim McCurdy, YinYangMoney.com
I don't think you can do anything server side after the user has decided to navigate away or browser is terminated. However you can write some JavaScript to prevent unloading of the current page where you can warn the user not to close it.
Second, use a small session timer that ticks every two minutes or so. Your session should timeout but when your Silverlight application is open and running in the browser, you should ping your server by writing some ping method that will keep your session alive every one minute.
So if your session is expiring (it didn't receive ping in the last 60 seconds), your session will be destroyed and you can write some cleanup code at the server's session end.
I had a similar requirement for an MVC app. What I did was use jQuery to subscribe to the unload event and make an ajax call to a controller action that killed the session:
$(window).unload(function() {
$.ajax({url: Url.Action("KillSession")});
});
public ActionResult KillSession()
{
Session.Abandon();
return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotModified);
}
精彩评论