如何:发送通知

更新:2007 年 11 月

每当用户应在您的应用程序中进行操作时,您都可以使用 Notification(如提示发送数据)。通常,当发生某个事件或满足某个条件时发送通知,但是为简单起见,此示例在单击一个按钮时显示一个通知。通过提供处理 ResponseSubmitted 事件的代码,您可以处理对通知的响应。

通知中的消息可以为纯文本或 HTML。HTML 使您可以发送一个包含复选框、按钮、列表和其他 HTML 元素的小的 HTML 窗体。此示例使用一个包含“提交”和“取消”按钮的简单窗体。

“取消”按钮由“cmd:2”标识,Windows CE 使用它来解除通知。如果 cmd:2 为消息气球中的 HTML 按钮或其他元素的名称,则不引发 ResponseSubmitted 事件。通知被解除,但是其图标被放置于标题栏上并且可以在以后响应。

发送通知

  1. 创建一个 Pocket PC Windows 应用程序。

  2. 将一个 NotificationButton 添加到窗体中。

  3. 创建一个 Notification 实例。

    Me.Notification1 = New Microsoft.WindowsCE.Forms.Notification
    
    this.notification1 = new Microsoft.WindowsCE.Forms.Notification();
    
  4. 添加下面的代码以处理按钮的 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;
    }
    
  5. 添加下面的代码以处理 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.
            }
        };
    

编译代码

此示例需要引用下面的命名空间:

请参见

任务

通知示例

参考

Notification

其他资源

Pocket PC 开发和 .NET Compact Framework