2009-06-14 93 views
0

我試圖在Windows Mobile中獲得自動消息回覆。我正在使用似乎第一次工作的MessageInterceptor類。但它似乎沒有工作的秒鐘消息!不知道是否必須有無限循環。我沒有太多的Windows Mobile開發經驗,所以請建議最好的方法。MessageInterceptor不會第二次使用窗口移動應用程序啓動

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; 

     } 



    } 
} 

感謝,

回答

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(); 
     } 
    } 
相關問題