Silverlight中的UserControl关闭事件 [英] UserControl Closing Event in Silverlight

查看:84
本文介绍了Silverlight中的UserControl关闭事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已经使用WCF Duplex概念在Silverlight中进行在线聊天。但是我无法处理Closing事件(意味着当用户关闭浏览器时应该通知WCF服务)。所以任何一个指南都可以如何处理关闭事件并从我的Loggedin用户列表中删除该用户。



谢谢

fayaz

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

推荐答案

你好

UserControl上没有Closing或Closed事件,因为它是一个控件/面板并放在其他控件上。



但您可以将UserControl放在Silverlight ChildWindow控件中,该控件的行为类似于WPF中的Window。 ChildWindow有启动和显示时可以附加的Closing和Closed事件。



如果你想在浏览器关闭时注销,有一个完全不同的方法。你需要附加到应用程序的退出 - 事件(App.xaml.cs)。



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
}





如果您还需要一些服务电话,则需要使用通过JavaScript处理浏览器关闭事件的ScriptableType。



ScriptableType定义为我称之为ShutdownManager的类:



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;
    }
}





您可以通过JavaScript调用Silverlight项目中的ShutdownManager类。 />




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

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





以下是调用的JavaScript函数:



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!


这篇关于Silverlight中的UserControl关闭事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆