Silverlight中使用自定义服务

来源:互联网 发布:伪装地理位置软件哪个 编辑:程序博客网 时间:2024/06/07 13:38

Silverlight中,可以通过所谓的自定义服务的方式 ,在后台运行一些特殊功能,例如定期检查远程网站资源等等 。

 

1. 编写一个自定义服务

using System.Windows;namespace BusinessApplication1{    public class MyService:IApplicationService,IApplicationLifetimeAware    {        #region IApplicationService 成员        public void StartService(ApplicationServiceContext context)        {                    }        public void StopService()        {                    }        #endregion        #region IApplicationLifetimeAware 成员        public void Exited()        {            MessageBox.Show("停止了");                    }        public void Exiting()        {            MessageBox.Show("正在停止");                    }        public void Started()        {            MessageBox.Show("启动了");        }        public void Starting()        {            MessageBox.Show("正在启动");                    }        #endregion    }}
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }

 

2. 注册该服务 ,必须在Application对象的构造器中注册

        public App()        {            this.ApplicationLifetimeObjects.Add(new MyService());            InitializeComponent();        }
.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }