Freigeben über


Gewusst wie: Senden einer Benachrichtigung

Aktualisiert: November 2007

Eine Notification kann überall da verwendet werden, wo ein Benutzer in der Anwendung aktiv werden muss. Sie können Benutzer beispielsweise zum Senden von Daten auffordern. Normalerweise werden Benachrichtigungen gesendet, wenn ein Ereignis auftritt oder eine Bedingung erfüllt wird. Der Einfachheit halber wird in diesem Beispiel eine Benachrichtigung angezeigt, wenn der Benutzer auf eine Schaltfläche klickt. Zur Verarbeitung von Antworten auf Benachrichtigungen können Sie Code bereitstellen, der das ResponseSubmitted-Ereignis behandelt.

Der Text einer Benachrichtigung kann im Nur-Text- oder im HTML-Format vorliegen. Das HTML-Format ermöglicht Ihnen das Senden eines kleinen HTML-Formulars mit Kontrollkästchen, Schaltflächen, Listen und anderen HTML-Elementen. In diesem Beispiel wird ein einfaches Formular mit den Schaltflächen Senden und Abbrechen verwendet.

Die Schaltfläche Abbrechen wird durch "cmd:2" dargestellt. Diesen Code verwendet Windows CE zum Schließen von Benachrichtigungen. Wenn cmd:2 der Name einer HTML-Schaltfläche oder eines anderen Elements in einer Meldungssprechblase ist, wird das ResponseSubmitted-Ereignis nicht ausgelöst. Die Benachrichtigung wird geschlossen, aber das zugehörige Symbol wird in die Titelleiste eingefügt und ermöglicht eine Beantwortung zu einem späteren Zeitpunkt.

So senden Sie eine Benachrichtigung

  1. Erstellen Sie eine Pocket PC-Windows-Anwendung.

  2. Fügen Sie dem Formular eine Notification und einen Button hinzu.

  3. Erstellen Sie eine Notification-Instanz.

    Me.Notification1 = New Microsoft.WindowsCE.Forms.Notification
    
    this.notification1 = new Microsoft.WindowsCE.Forms.Notification();
    
  4. Fügen Sie den folgenden Code hinzu, um das Click-Ereignis der Schaltfläche zu behandeln.

    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. Fügen Sie den folgenden Code hinzu, um das ResponseSubmitted-Ereignis zu behandeln.

    ' 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.
            }
        };
    

Kompilieren des Codes

Für dieses Beispiel sind Verweise auf die folgenden Namespaces erforderlich:

Siehe auch

Aufgaben

Beispiel für Benachrichtigungen

Referenz

Notification

Weitere Ressourcen

Pocket PC-Entwicklung und .NET Compact Framework