Click here to Skip to main content
11,430,109 members (73,526 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WCF Silverlight
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.

Thanks
fayaz
Posted 12-Dec-12 4:53am

1 solution

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

Solution 1

Hello
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;
 
    InitializeComponent();
}
 
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":
[ScriptableType]
public class ShutdownManager
{
    [ScriptableMember]
    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!
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150428.2 | Last Updated 16 Dec 2012
Copyright © CodeProject, 1999-2015
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