自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Windows Phone開發(fā)(44):推送通知第二集之磁貼通知

移動開發(fā)
重點就是,Action="Clear",但要注意,磁貼正面的背景圖不能清除。好,再來新建一個WP應(yīng)用,這回要做客戶端。直接新建即可,XAML文檔不用改,因為我們不需要界面設(shè)計了,直打開后臺代碼吧。

前面我們說了***個類型——Toast通知,這玩意兒不知大家是不是覺得很新鮮,以前玩.NET編程應(yīng)該沒接觸過吧?

其實這東西絕對不復(fù)雜,只是剛接觸的時候會有點莫名罷了,Toast通知和今天要說的磁貼通知,都有一個共同點,那就是格式都規(guī)定死了D。

本質(zhì)就是向特定的URI地址POST一個XML文檔罷了,相信很多人都會,如果你還不會,真的,要補一補基礎(chǔ)課了。

多說無益,還是快點切入主題,開門見水吧。

首先,我們要知道我們在服務(wù)器端要POST什么樣的XML文檔,來,一起來看看。

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <wp:Notification xmlns:wp="WPNotification">   
  3.   <wp:Tile ID="導(dǎo)航URI">   
  4.     <wp:BackgroundImage>正面背景圖片</wp:BackgroundImage>   
  5.     <wp:Count>計數(shù)器</wp:Count>   
  6.     <wp:Title>正面標題</wp:Title>   
  7.     <wp:BackBackgroundImage>背面背景圖片</wp:BackBackgroundImage>   
  8.     <wp:BackTitle>背面標題</wp:BackTitle>   
  9.     <wp:BackContent>背面內(nèi)容</wp:BackContent>   
  10.   </wp:Tile>   
  11. </wp:Notification> 

前面關(guān)于磁貼的內(nèi)容,大家有印象吧?

磁帖者,有正面的標題、背景圖、計數(shù)器;背面有標題、背景圖和正文。有印象就好,不用我打水口槍。

來吧,我們通過一個現(xiàn)場演練來體會體會吧。

先做服務(wù)器端,這回我選擇用ASP.NET,不要告訴我你不會。

啟動VS,建一個ASP.NET網(wǎng)站,然后,把default.aspx改造一下,如果你嫌生成的代碼不好看,可以把文件刪除,然后新建一個頁面。

好了,頁面布局嘛,我貼一下HTML就行了。

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>   
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  3. <html xmlns="http://www.w3.org/1999/xhtml">   
  4. <head runat="server">   
  5.     <title></title>   
  6. </head>   
  7. <body>   
  8.     <form id="form1" runat="server">   
  9.     <div>   
  10.         <div>   
  11.             目標URI:   
  12.             <asp:TextBox ID="txtURI" runat="server" Width="911px"></asp:TextBox>   
  13.         </div>   
  14.         <div>   
  15.             <table border="0">   
  16.                 <tr>   
  17.                     <td>正面背景:</td>   
  18.                     <td>   
  19.                         <asp:TextBox ID="txtBackImg" runat="server" Width="316px"></asp:TextBox></td>   
  20.                 </tr>   
  21.                 <tr>   
  22.                     <td>正面標題:</td>   
  23.                     <td>   
  24.                         <asp:TextBox ID="txtTitle" runat="server" Width="316px"></asp:TextBox>   
  25.                     </td>   
  26.                 </tr>   
  27.                 <tr>   
  28.                     <td>計數(shù):</td>   
  29.                     <td>   
  30.                         <asp:TextBox ID="txtCount" runat="server" Width="313px"></asp:TextBox>   
  31.                     </td>   
  32.                 </tr>   
  33.                 <tr>   
  34.                     <td>背面背景:</td>   
  35.                     <td>   
  36.                         <asp:TextBox ID="txtBackBackImg" runat="server" Width="316px"></asp:TextBox>   
  37.                     </td>   
  38.                 </tr>   
  39.                 <tr>   
  40.                     <td>背面標題:</td>   
  41.                     <td>   
  42.                         <asp:TextBox ID="txtBackTitle" runat="server" Width="321px"></asp:TextBox>   
  43.                     </td>   
  44.                 </tr>   
  45.                 <tr>   
  46.                     <td>背面正文:</td>   
  47.                     <td>   
  48.                         <asp:TextBox ID="txtBackContent" runat="server" Width="309px"></asp:TextBox>   
  49.                     </td>   
  50.                 </tr>   
  51.             </table>   
  52.             <div style="margin-left:20px; margin-top:10px;">   
  53.                 <asp:Button ID="btnSend" runat="server" Text="發(fā)送" onclick="btnSend_Click" /></div>   
  54.         </div>   
  55.         <div style=" margin-top:20px;">   
  56.             <asp:TextBox ID="txtRes" runat="server" Height="155px" TextMode="MultiLine"    
  57.                 Width="729px"></asp:TextBox>   
  58.         </div>   
  59.     </div>   
  60.     </form>   
  61. </body>   
  62. </html>   

還是別少了后臺代碼。

  1. /*  
  2.  <?xml version="1.0" encoding="utf-8" ?>  
  3. <wp:Notification xmlns:wp="WPNotification">  
  4.   <wp:Tile ID="導(dǎo)航URI">  
  5.     <wp:BackgroundImage>正面背景圖片</wp:BackgroundImage>  
  6.     <wp:Count>計數(shù)器</wp:Count>  
  7.     <wp:Title>正面標題</wp:Title>  
  8.     <wp:BackBackgroundImage>背面背景圖片</wp:BackBackgroundImage>  
  9.     <wp:BackTitle>背面標題</wp:BackTitle>  
  10.     <wp:BackContent>背面內(nèi)容</wp:BackContent>  
  11.   </wp:Tile>  
  12. </wp:Notification>  
  13.  * 清除磁貼的屬性值  
  14.  <?xml version="1.0" encoding="utf-8" ?>  
  15. <wp:Notification xmlns:wp="WPNotification">  
  16.   <wp:Tile ID="導(dǎo)航URI">  
  17.     <wp:BackgroundImage></wp:BackgroundImage>  
  18.     <wp:Count Action="Clear"></wp:Count>  
  19.     <wp:Title Action="Clear"></wp:Title>  
  20.     <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>  
  21.     <wp:BackTitle Action="Clear"></wp:BackTitle>  
  22.     <wp:BackContent Action="Clear"></wp:BackContent>  
  23.   </wp:Tile>  
  24. </wp:Notification>  
  25.  * HTTP標頭  
  26.  X-WindowsPhone-Target: token  
  27. X-NotificationClass:1  
  28. 1 立即發(fā)送  
  29. 11 450秒發(fā)送  
  30. 21  900秒發(fā)送  
  31.  */   
  32. using System;   
  33. using System.Collections.Generic;   
  34. using System.Linq;   
  35. using System.Web;   
  36. using System.Web.UI;   
  37. using System.Web.UI.WebControls;   
  38. using System.Net;   
  39. using System.Net.Mime;   
  40. using System.IO;   
  41. using System.Text;   
  42. public partial class _Default : System.Web.UI.Page   
  43. {   
  44.     protected void Page_Load(object sender, EventArgs e)   
  45.     {   
  46.     }   
  47.     protected void btnSend_Click(object sender, EventArgs e)   
  48.     {   
  49.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtURI.Text);   
  50.         request.Method = WebRequestMethods.Http.Post;   
  51.         // 加上HTTP標頭   
  52.         request.Headers.Add("X-WindowsPhone-Target""token");   
  53.         request.Headers.Add("X-NotificationClass""1");   
  54.         // 拼接內(nèi)容,XML文檔   
  55.         string Msg = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +   
  56.                      "<wp:Notification xmlns:wp=\"WPNotification\">" +   
  57.                         "<wp:Tile>" +   
  58.                             "<wp:BackgroundImage>" + txtBackImg.Text + "</wp:BackgroundImage>" +   
  59.                             "<wp:Count>" + txtCount.Text + "</wp:Count>" +   
  60.                             "<wp:Title>" + txtTitle.Text + "</wp:Title>" +   
  61.                             "<wp:BackBackgroundImage>" + txtBackBackImg.Text + "</wp:BackBackgroundImage>" +   
  62.                             "<wp:BackTitle>" + txtBackTitle.Text + "</wp:BackTitle>" +   
  63.                             "<wp:BackContent>" + txtBackContent.Text + "</wp:BackContent>" +   
  64.                         "</wp:Tile>" +   
  65.                       "</wp:Notification>";   
  66.         byte[] buffer = Encoding.UTF8.GetBytes(Msg);   
  67.         request.ContentType = MediaTypeNames.Text.Xml;   
  68.         // POST數(shù)據(jù)要記得設(shè)置內(nèi)容長度   
  69.         request.ContentLength = buffer.Length;   
  70.         // 寫入流   
  71.         using (Stream stream = request.GetRequestStream())   
  72.         {   
  73.             stream.Write(buffer, 0, buffer.Length);   
  74.         }   
  75.         // 接收回應(yīng)   
  76.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();   
  77.         // 讀出響應(yīng)的HTTP頭   
  78.         string headers = "";   
  79.         foreach (string key in response.Headers.AllKeys)   
  80.         {   
  81.             headers += key + " : " + response.Headers.Get(key) + "\r\n";   
  82.         }   
  83.         txtRes.Text = headers;   
  84.     }   
  85. }   

補充一下,上面代碼中,前面的注釋我已經(jīng)寫上了,其實MSDN上都有,我想很多人不看,我說一下,如果你打算清除磁貼某些屬性的值,如標題等,這可以用以下的XML文檔。

  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <wp:Notification xmlns:wp="WPNotification">   
  3.   <wp:Tile ID="導(dǎo)航URI">   
  4.     <wp:BackgroundImage></wp:BackgroundImage>   
  5.     <wp:Count Action="Clear"></wp:Count>   
  6.     <wp:Title Action="Clear"></wp:Title>   
  7.     <wp:BackBackgroundImage Action="Clear"></wp:BackBackgroundImage>   
  8.     <wp:BackTitle Action="Clear"></wp:BackTitle>   
  9.     <wp:BackContent Action="Clear"></wp:BackContent>   
  10.   </wp:Tile>   
  11. </wp:Notification>  

重點就是,Action="Clear",但要注意,磁貼正面的背景圖不能清除。

好,再來新建一個WP應(yīng)用,這回要做客戶端。

直接新建即可,XAML文檔不用改,因為我們不需要界面設(shè)計了,直打開后臺代碼吧。

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Net;   
  5. using System.Windows;   
  6. using System.Windows.Controls;   
  7. using System.Windows.Documents;   
  8. using System.Windows.Input;   
  9. using System.Windows.Media;   
  10. using System.Windows.Media.Animation;   
  11. using System.Windows.Shapes;   
  12. using Microsoft.Phone.Controls;   
  13. using Microsoft.Phone.Notification;   
  14. namespace WPClient   
  15. {   
  16.     public partial class MainPage : PhoneApplicationPage   
  17.     {   
  18.         // 構(gòu)造函數(shù)   
  19.         public MainPage()   
  20.         {   
  21.             InitializeComponent();   
  22.             HttpNotificationChannel Channel = null;   
  23.             // 通道名,隨便弄一個,不要與其它應(yīng)用程序重復(fù)   
  24.             string Channel_Name = "TileNoftification";   
  25.             // 在現(xiàn)有的通道里面找找,看能不能找著?   
  26.             Channel = HttpNotificationChannel.Find(Channel_Name);   
  27.             if (Channel == null)   
  28.             {   
  29.                 // 找不到,那就新建一個唄   
  30.                 Channel = new HttpNotificationChannel(Channel_Name);   
  31.                 // 打開通道前要先注冊事件處理,為什么?自己想一下吧   
  32.                 // 就是因為ChannelUriUpdated事件,如不這樣,   
  33.                 // 當(dāng)***次獲取URI時你就得不到更新通知   
  34.                 Channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);   
  35.                 // 出事了,總得向上級匯報一下吧?   
  36.                 Channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);   
  37.                 // 登記注冊完畢,著手辦喜事   
  38.                 Channel.Open();   
  39.                 // 辦喜事別忘記了發(fā)請?zhí)?調(diào)用BindToShellTile   
  40.                 Channel.BindToShellTile();   
  41.             }   
  42.             else   
  43.             {   
  44.                 // 如果找到了通道,還是要注冊一下事件   
  45.                 // 老夫妻也可以再拍一回婚紗照吧?   
  46.                 Channel.ChannelUriUpdated+=new EventHandler<NotificationChannelUriEventArgs>(Channel_ChannelUriUpdated);   
  47.                 Channel.ErrorOccurred+=new EventHandler<NotificationChannelErrorEventArgs>(Channel_ErrorOccurred);   
  48.                 // 把住址告訴人家,相冊做好之后,數(shù)碼沖印店送貨上門   
  49.                 System.Diagnostics.Debug.WriteLine("URI: {0}", Channel.ChannelUri.ToString());   
  50.             }   
  51.         }   
  52.         void Channel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)   
  53.         {   
  54.             // 向上級匯報一下錯誤   
  55.             Dispatcher.BeginInvoke(() =>   
  56.                 {   
  57.                     MessageBox.Show(e.Message);   
  58.                 });   
  59.         }   
  60.         void Channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)   
  61.         {   
  62.             // 搬家了記得通知一下大家新住址   
  63.             Dispatcher.BeginInvoke(() =>   
  64.                 {   
  65.                     System.Diagnostics.Debug.WriteLine("URI: {0}", e.ChannelUri.ToString());   
  66.                 });   
  67.         }   
  68.     }   
  69. }   

先動行WP端,當(dāng)然,同時運行兩個都可以了。

在“輸出”窗口中,把這個URI復(fù)制到服務(wù)器端的網(wǎng)頁上。

接著,按模擬器的“開始”按鈕,來到“開始”屏幕,向右滑動,看到應(yīng)用程序列表,在本應(yīng)用程序上長按,從彈出的菜單中選“固定到開始屏幕”.

然后,回到服務(wù)器端頁面,填好所有參數(shù),點擊“發(fā)送”。看結(jié)果。

都看到效果了?

圖片可以自己準備,png格式,173*173,隨便用畫圖工具搞兩下就行了,只是為了測試,把圖片加到項目后,設(shè)置以下屬性就行了。

OK,大家照著上面的多練幾次,一定有感覺的,我不想講理論的東西,因為沒什么用。

責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

2013-04-25 15:15:41

Windows PhoWindows PhoWindows Pho

2013-04-25 14:05:20

Windows PhoWindows PhoWindows Pho

2012-08-16 11:31:30

Windows Pho

2013-02-19 09:04:32

Windows 8開發(fā)

2021-06-11 07:30:30

并發(fā)高并發(fā)內(nèi)存

2013-04-17 11:21:59

Windows PhoWindows Pho

2020-03-27 18:00:37

微軟Windows 10操作系統(tǒng)

2022-05-13 14:16:05

云計算

2018-03-26 21:31:30

深度學(xué)習(xí)

2022-03-18 12:27:21

Android開發(fā)者功能

2011-08-03 16:45:09

iPhone APNS 推送通知

2021-06-16 09:49:14

Windows 11微軟動態(tài)磁貼

2011-04-06 09:33:40

Push動互聯(lián)網(wǎng)

2013-07-31 13:13:50

Windows PhoMVVM模式

2010-08-01 15:16:41

Android

2011-09-21 17:07:57

WP7

2013-04-24 13:19:06

Windows Pho動畫DoubleAni

2013-04-24 13:31:59

Windows Pho動畫之ColorAni

2013-04-19 17:11:02

Windows PhoWindows Pho

2013-04-24 13:43:10

Windows Pho動畫PointAnim
點贊
收藏

51CTO技術(shù)棧公眾號