Share via


Comment : envoyer une notification

Mise à jour : novembre 2007

Vous pouvez utiliser une Notification chaque fois qu'un utilisateur doit agir dans votre application, par exemple une invite à envoyer des données. En général, vous envoyez une notification lorsqu'un événement se produit ou qu'une condition est remplie mais, pour des raisons de simplicité, cet exemple affiche une notification lorsque vous cliquez sur un bouton. Vous pouvez traiter des réponses aux notifications en fournissant le code pour gérer l'événement ResponseSubmitted.

Le message d'une notification peut être du texte brut ou du code HTML. Le code HTML permet d'envoyer un petit formulaire HTML qui contient des cases à cocher, boutons, listes et d'autres éléments HTML. Cet exemple utilise un formulaire simple avec les boutons Envoyer et Annuler.

Le bouton Annuler est identifié par "cmd:2" que Windows CE utilise pour fermer les notifications. Si cmd:2 est le nom d'un bouton HTML ou d'un autre élément dans une bulle de message, l'événement ResponseSubmitted n'est pas déclenché. La notification est fermée, mais son icône est placée dans la barre de titre et vous pouvez y répondre plus tard.

Pour envoyer une notification

  1. Créez une application Windows Pocket PC.

  2. Ajoutez Notification et Button au formulaire.

  3. Créez une instance de Notification.

    Me.Notification1 = New Microsoft.WindowsCE.Forms.Notification
    
    this.notification1 = new Microsoft.WindowsCE.Forms.Notification();
    
  4. Ajoutez le code suivant pour gérer l'événement Click du bouton.

    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. Ajoutez le code suivant pour gérer l'événement 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.
            }
        };
    

Compilation du code

Cet exemple nécessite des références aux espaces de noms suivants :

Voir aussi

Tâches

Notification, exemple

Référence

Notification

Autres ressources

Développement Pocket PC et le .NET Compact Framework