消息拦截器没有第二次启动窗口移动应用程序

时间:2009-06-14 01:42:11

标签: c# windows-mobile

我正在尝试在Windows Mobile中进行自动回复回复。我正在使用MessageInterceptor类,这似乎是第一次工作。但它似乎不适用于秒信息!不确定我是否必须有无限循环。我没有很多Windows Mobile开发经验,所以请提出最佳方法。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;


namespace TextMessage3
{
    public partial class Form1 : Form
    {

        protected MessageInterceptor smsInterceptor = null;

        public Form1()
        {
            InitializeComponent();
            debugTxt.Text = "Calling Form cs";
            //Receiving text message
            MessageInterceptor interceptor = new MessageInterceptor(InterceptionAction.NotifyandDelete);
            interceptor.MessageReceived += SmsInterceptor_MessageReceived;                  
        }

        public void SmsInterceptor_MessageReceived(object sender, 
         MessageInterceptorEventArgs e)
        {
              SmsMessage msg = new SmsMessage();
              msg.To.Add(new Recipient("James", "+16044352345"));
              msg.Body = "Congrats, it works!";
              msg.Send();
              //Receiving text message
              MessageInterceptor interceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete);
              interceptor.MessageReceived += SmsInterceptor_MessageReceived;   

        }



    }
}

谢谢,

1 个答案:

答案 0 :(得分:5)

看起来你的MessageInteceptor类在获取第二条消息之前就已经处理好了,因为一旦你离开构造函数或事件处理程序,对该对象的唯一引用就会消失。而不是每次收到消息时都创建一个新对象,只需在构造函数中创建一个并将其设置为您的成员变量。每次收到消息时,都应该调用SmsInterceptor_MessageReceived函数。

public partial class Form1 : Form
    {

        protected MessageInterceptor smsInterceptor = null;

        public Form1()
        {
            InitializeComponent();
            debugTxt.Text = "Calling Form cs";
            //Receiving text message
            this.smsInterceptor  = new MessageInterceptor(InterceptionAction.NotifyandDelete);
            this.smsInterceptor.MessageReceived += this.SmsInterceptor_MessageReceived;                  
        }

        public void SmsInterceptor_MessageReceived(object sender, 
         MessageInterceptorEventArgs e)
        {
              SmsMessage msg = new SmsMessage();
              msg.To.Add(new Recipient("James", "+16044352345"));
              msg.Body = "Congrats, it works!";
              msg.Send();  
        }
    }