Silverlight为什么要使用程序扩展服务

应用程序扩展服务 是什么?为什么要使用?一开始看的时候我也很疑惑。于是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秒就报时来模拟一个读取新闻或者其他定时处理之类的东西

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
    

/// <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注册 

 
   
< Application xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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 >

或者在代码中添加

 

 
   
public App() {
this .ApplicationLifetimeObjects.Add( new ASvc());
}

 

3调用 

 

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
    
ASvc.Current.timeOver += new System.EventHandler(aSvc_timeOver);
}

void aSvc_timeOver( object sender, System.EventArgs e) {
MessageBox.Show(sender
+ " 7秒钟啦 " );
}

 

 

测试的时候,发现,先执行页面初始化再执行 扩展服务类的构造函数。

 

 

 1  当需要整个程序的背景线程执行东西的时候。

         比如邮件获取,广告等等。

 2  操作比较频繁的时候可以考虑。

 3  如果不这样做,会频繁实例化然后销毁的时候。

 

他 使用扩展服务更具模块化的特性。

显得耦合更加小。从全局的一个容器中取出来就来了。

 

使用它的便利

他帮你完成了初始化和关闭(接口调用)

帮你解析动态配置  APP..XAML

 

但是我在网上搜的时候发现34的用法都不一样。不知道5会不会改呢? 

使用它的优势还是很明显的。

其实园子里 webabcd 已经介绍了这些。。。(他基本都说完了。。。)

但是没有说明具体的用法,

 

参考

http://www.cnblogs.com/webabcd/archive/2009/09/08/1562249.html

Silverlight 3.0: Application Extensibility

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值