Click here to Skip to main content
15,886,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

Thanks
fayaz
Posted

1 solution

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).

C#
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":
C#
[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.

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


And here is the JavaScript function that is called:
C#
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900