应用程序扩展服务 是什么?为什么要使用?一开始看的时候我也很疑惑。于是Google了一下。
BTW:baidu似乎不够用...
Sometimes working with silverlight you need to have a sort of service, always available for global tasks or
working in background notifing the main thread when something happen.
-------Andrea Boschin
具体用法:
1首先继承实现一个每7秒就报时来模拟一个读取新闻或者其他定时处理之类的东西
/// <summary>
/// 每7秒钟报一次时间
/// </summary>
public class ASvc : IApplicationService {
/// <summary>
/// 自己 单例模式
/// </summary>
public static ASvc Current { get ; set ; }
public event EventHandler timeOver;
void dis_Tick( object sender, EventArgs e) {
timeOver(sender, e);
}
public ASvc() {
Current = this ;
}
#region IApplicationService Members
private DispatcherTimer dis;
public void StartService(ApplicationServiceContext context) {
MessageBox.Show( " StartService " + context.ApplicationInitParams);
dis = new DispatcherTimer();
dis.Interval = TimeSpan.FromSeconds( 7 );
dis.Tick += new EventHandler(dis_Tick);
dis.Start();
}
public void StopService() {
MessageBox.Show( " StopService " );
dis.Stop();
dis.Tick -= dis_Tick;
}
#endregion
}
2注册
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class ="SLstudy.App"
xmlns:svc ="clr-namespace:SLstudy"
>
< Application.ApplicationLifetimeObjects >
< svc:ASvc />
</ Application.ApplicationLifetimeObjects >
< Application.Resources >
</ Application.Resources >
</ Application >
或者在代码中添加
this .ApplicationLifetimeObjects.Add( new ASvc());
}
3调用
}
void aSvc_timeOver( object sender, System.EventArgs e) {
MessageBox.Show(sender + " 7秒钟啦 " );
}
测试的时候,发现,先执行页面初始化再执行 扩展服务类的构造函数。
1 当需要整个程序的背景线程执行东西的时候。
比如邮件获取,广告等等。
2 操作比较频繁的时候可以考虑。
3 如果不这样做,会频繁实例化然后销毁的时候。
他 使用扩展服务更具模块化的特性。
显得耦合更加小。从全局的一个容器中取出来就来了。
使用它的便利
1 他帮你完成了初始化和关闭(接口调用)
2 帮你解析动态配置 APP..XAML
但是我在网上搜的时候发现3和4的用法都不一样。不知道5会不会改呢?
使用它的优势还是很明显的。
其实园子里 webabcd 已经介绍了这些。。。(他基本都说完了。。。)
但是没有说明具体的用法,
参考
http://www.cnblogs.com/webabcd/archive/2009/09/08/1562249.html