WP7基础学习第十二讲

多任务的模拟
基本知识\Tombstone\Push Notification

(注:对于WP7基础的学习的介绍,就要结束啦!我这里有十五个视频,共15讲,这是第十二讲!下面将会介绍关于淘宝的Open API!)

基本知识:
应用程序模型只支持在前台执行;如果另外一个程序在你的程序正在运行的时候启动,程序收到一个将要被终止的时间通知;应用从前台离开的时候并没有被马上终止,如果需要保留状态,就要自己编写一些逻辑来处理这些情况;在程序关闭状态下,可以通过Web Service向程序发送信息以及更新程序状态

Tombstone:(墓碑机制,保存状态)

1.可执行模式:Tombstoning\Page State\Application state\Persistent data\Transient state\Tombstoning和Transient state模式下模拟多任务,在Tombston模式下,应用被操作系统Terminate,就需要保存Transient state,以备于应用复活后回复应用原状态

2.Lifecycle包括:Lauching\Running\Closing\Deactivating\Activating这五种运行状态,Deactivating\Activating就是Tombstone下的运行态。在这两个运行态下可以完成唤醒后的状态或数据的转移

3.PhoneApplicationService,保存状态信息

在App.xaml.cs中
(using Microsoft.Phone.Shell;)
对应的五种状态的委托事件写入:
Debug.WriteLine("Lauching");
Debug.WriteLine("Activated");
Debug.WriteLine("DeActivated");

在对应的后台代码:
(using Microsoft.Phone.Shell;)

protected override void OnNavigateFrom()
{
//保存状态
 if(PhoneApplicationService.Current.State.ContainKey("Test"))
 {
   PhoneApplicationService.Current.State.Remove("Test");
 }
 PhoneApplicationService.Current.State["Test"] =this.textBox1.Text;
 base.OnNavigateFrom(e);
}

protected override void OnNavigateTo()
{
 if(PhoneApplicationService.Current.State.ContainKey("Test"))
 this.textBox1.Text=PhoneApplicationService.Current.State ["Test"] as string;
 base.OnNavigateTo(e);
}

Push Notification:
为手机端应用和WebService之间建立了一条专用、持久、稳定的通道来推送通知。当通道建立后,手机端应用可以接受WebSerice的任何信息

分类:
1.Tile Notification:可以改变Quick Lanuch area内的图形内容(图片、文字等)的方式,但需要程序被Pin to Start
2.Toast Notification;在屏幕上面可以显示一个提示栏的方式,当点击提示栏可以打开应用程序
3.Raw Notification:直接使用Http方式来接受(http polling)通知的方式。是不可见的,以后台方式传送通知


使手机使用条件,代码:

1.创建客户端:

HttpChannel=new HttpNotificationChannel(Channel.Name,"TestService");
httpChannel.Open();
//绑定Notification(绑定通道)
httpChannel.BingToShellToast();
httpChannel.BingToShellTile(uris);
//获取Notification channel URL(获取通道的URI)
httpChannel.ChannelUriUpdate+=new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated);
//获取Raw Notification(监听Raw信息)
httpChannel.HttpNotificationReceived+=new EvebtHandler<HttpNotificationEventArgs>(httpChannel_HttpNotificationReceived);
//获取Toast Notification(监听Toast信息)
httpChannel.ShellToastNotificationReceived+=new EventHandle<NotificationEventArgs>(httpChannnel_ShellToastNotificationReceived);
//获取Push Notification error message(监听错误信息)
httpChannel.ErrorOccurred+=new EventHandle<NotificationChannelErrorEventArgs>(httpChannel_ExceptionOccurred);
//对于Tile Notification是由系统来接收的,所以没有相应的Event

2.创建服务器端
都是以Http方式发送不同的通知;需要排至相应的参数,告诉Push Notification Service所发送的类型是什么

HttpWebRequest request=(HttpWebRequest)WebRequest.Create(channelUri);
request.Method=WebRequestMethods.Http.Post;
request.ContentType="text/xml:charset=utf-8";
request.ContentLength=notificationmessage.Length;
request.Headers["X-MessageID"]=Guid.NewGuid().ToString();
//设置发送的Notification类型
request.Headers["X-WindowsPhone-Target"]="toast";
request.Headers["X-NotificationClass"]="2";

//Response数据(服务器返回数据)
response.StatusCode//OK 表示成功
response.Headers[X-DeviceConnectionStatus]
response.Headers[X-SubscriptionStatus]
response.Headers[X-NotificationStatus]


Tile Notification:制用Tile图片(Count\Title\Background)
设置:在程序properties中设置Tile初始文字和图片

TileMessage

"Content-Type:text/xml\r\nX-WindowsPhone-Target:token\r\n\r\n"
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification">
 <wp:Tile>
  <wp:BackgroundImage>
   <background image path>//这里填写图片所在的相对位置
  </wp:BackgroundImage>
  <wp:Coount>
   <count>
  </wp:Count>
  <wp:Title>
   <title>
  </wp:Title>
 </wp:Tile>
</wp:Notification>

Toast Notification:

ToastMessage
"Content-Type:text/xml\r\nX-WindowsPhone-Target:toast\r\n\r\n"
<?xml version="1.0" encoding="utf-8"?>
<wp:Notification xmlns:wp="WPNotification">
 <wp:Toast>
  <wp:Text1>
   <string>
  </wp:Text1>
  <wp:Text2>
   <string>
  </wp:Text2>
 </wp:Toast>
</wp:Notifition>

Raw Notification:

Raw Message
request.Headers[X-NotificationClass]

慎重使用:Push Notification,因为有些信息可能收不到(15个)

原文地址:https://www.cnblogs.com/SanMaoSpace/p/2139374.html