HOW TO:傳送通知
更新:2007 年 11 月
每當使用者需在應用程式中採取必要動作時 (例如,被提示要傳送資料),您都可以使用 Notification。通常,您會在事件發生或條件符合時傳送告知,但為了簡單起見,這個範例會在按下按鈕時顯示告知。您可以透過提供程式碼來處理 ResponseSubmitted 事件,以便處理告知的回應。
告知中的訊息可以是純文字或 HTML。HTML 可讓您傳送內含核取方塊、按鈕、清單和其他 HTML 項目的小型 HTML 表單。這個範例會使用內含 [送出] 和 [取消] 按鈕的簡易表單。
[取消] 按鈕可利用 "cmd:2" 來辨識,而 Windows CE 會使用此按鈕來解除告知。如果 cmd:2 是 HTML 按鈕的名稱或訊息提示中的其他項目,則不會引發 ResponseSubmitted 事件。告知雖已解除,但其圖示會放在標題列上並可於稍後回應。
若要傳送告知
建立 Pocket PC Windows 應用程式。
將 Notification 和 Button 加入至表單。
建立 Notification 執行個體。
Me.Notification1 = New Microsoft.WindowsCE.Forms.Notification
this.notification1 = new Microsoft.WindowsCE.Forms.Notification();
加入下列程式碼,以處理按鈕的 Click 事件。
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' Use a StringBuilder for better performance. Dim HTMLString As New StringBuilder HTMLString.Append("<html><body>") HTMLString.Append("Submit data?") HTMLString.Append("<form method=\'GET\' action=notify>") HTMLString.Append("<input type='submit'>") HTMLString.Append( _ "<input type=button name='cmd:2' value='Cancel'>") HTMLString.Append("</body></html>") ' Set the Text property to the HTML string. Notification1.Text = HTMLString.ToString() Dim IconStream As New FileStream(".\My Documents\notify.ico", _ FileMode.Open, FileAccess.Read) Notification1.Icon = new Icon(IconStream, 16, 16) Notification1.Caption="Notification Demo" Notification1.Critical = false ' Display icon up to 10 seconds. Notification1.InitialDuration = 10 Notification1.Visible = true End Sub
private void button1_Click(object sender, System.EventArgs e) { StringBuilder HTMLString = new StringBuilder(); HTMLString.Append("<html><body>"); HTMLString.Append("Submit data?"); HTMLString.Append("<form method=\'GET\' action=notify>"); HTMLString.Append("<input type='submit'>"); HTMLString.Append("<input type=button name='cmd:2' value='Cancel'>"); HTMLString.Append("</body></html>"); //Set the Text property to the HTML string. notification1.Text = HTMLString.ToString(); FileStream IconStream = new FileStream(".\\My Documents\\notify.ico", FileMode.Open, FileAccess.Read); notification1.Icon = new Icon(IconStream, 16, 16); notification1.Caption="Notification Demo"; notification1.Critical = false; // Display icon up to 10 seconds. notification1.InitialDuration = 10; notification1.Visible = true; }
加入下列程式碼,以處理 ResponseSubmitted 事件。
' When a ResponseSubmitted event occurs, this event handler ' parses the response to determine values in the HTML form. Private Sub Notification1_ResponseSubmitted(ByVal sender As Object, _ ByVal resevent As Microsoft.WindowsCE.Forms.ResponseSubmittedEventArgs) _ Handles Notification1.ResponseSubmitted If resevent.Response.Substring(0,6) = "notify" Then ' Add code here to respond to the notification. End If End Sub
// When a ResponseSubmitted event occurs, this event handler // parses the response to determine values in the HTML form. notification1.ResponseSubmitted += delegate (object obj, ResponseSubmittedEventArgs resevent) { if (resevent.Response.Substring(0,6) == "notify") { // Add code here to respond to the notification. } };
編譯程式碼
這個範例需要下列命名空間的參考: