Click here to Skip to main content
12,999,833 members (48,780 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi I have Used WCF Duplex concept for online chatting in Silverlight.But I am unable to handle the Closing event(Means when a user is closing the Browser so It should be notified to the WCF service).So Can any one guide How to handle the close event and delete that user from my Loggedin List of User.

Posted 12-Dec-12 3:53am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There is no Closing or Closed event on a UserControl because it is a control/panel and placed along other controls.

But you may place your UserControl in a Silverlight ChildWindow control, which behaves like a Window in WPF. The ChildWindow has Closing and Closed events you can attach to when initiating and showing it.

If you want to log out when the Browser closes, there is a completely different approach. You will need to attach to the "Exit"-event on the application (App.xaml.cs).

public App()
    this.Startup += this.Application_Startup;
    this.Exit += this.Application_Exit;
private void Application_Startup(object sender, StartupEventArgs e)
    HtmlPage.RegisterScriptableObject("ShutdownManager", new ShutdownManager());
private void Application_Exit(object sender, EventArgs e)
    // Clean-up here

If you need some service-calls also, you will need to use the "ScriptableType" to handle the browser's close event via JavaScript.

The "ScriptableType" is defined as a class I call "ShutdownManager":
public class ShutdownManager
    public Boolean CleanUp()
        // Further clean-up here.
        // You can also modofy this mothod to return String, and give a message to the browser.
        return true;

You call the "ShutdownManager" class in the Silverlight-project via JavaScript.

<body onbeforeunload="return doClose();"></body>

And here is the JavaScript function that is called:
function doClose() {
    var plugin = document.getElementById('slcontrol'); // <-- ENTER ID OF YOUR SILVERLIGHT APPLICATION OBJECT HERE
    var shutdownManager = plugin.content.ShutdownManager;
    var cleanupResult = shutdownManager.CleanUp();
    if (cleanupResult)
        alert('You have been logged out!');

Hope that helps your problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 16 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100