Cómo: Enviar una notificación
Actualización: noviembre 2007
Puede utilizar Notification cada vez que un usuario deba realizar una acción en su aplicación, como solicitar el envío de datos. Normalmente, se envía una notificación cuando tiene lugar un evento o se cumple una condición; sin embargo, para hacerlo más sencillo, en este ejemplo se muestra una notificación cuando se hace clic en un botón. Puede procesar las respuestas a las notificaciones proporcionando el código necesario para controlar el evento ResponseSubmitted.
El mensaje de una notificación puede ser texto sin formato o HTML. HTML permite enviar un formulario HTML pequeño que contiene casillas, botones, listas y otros elementos HTML. Este ejemplo utiliza un formulario simple con los botones Enviar y Cancelar.
El botón Cancelar se identifica mediante "cmd:2", que es lo que utiliza Windows CE para descartar las notificaciones. Si cmd:2 es el nombre de un botón HTML u otro elemento de un globo de mensaje, no se provoca el evento ResponseSubmitted. En este caso, se descarta la notificación, pero su icono se coloca en la barra de título para poder responder a ella más adelante.
Para enviar una notificación
Cree una aplicación para Windows de Pocket PC.
Agregue una notificación Notification y un botón Button al formulario.
Cree una instancia Notification.
Me.Notification1 = New Microsoft.WindowsCE.Forms.Notification
this.notification1 = new Microsoft.WindowsCE.Forms.Notification();
Agregue el código siguiente para controlar el evento Click del botón.
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; }
Agregue el código siguiente para controlar el evento 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. } };
Compilar el código
Para este ejemplo se requieren referencias a los siguientes espacios de nombres: