2012-04-06 57 views
0

下面的代码将工作的Click事件处理程序,但我不能让它自动发送SMStextmesage后台服务异步

async void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     SmsDevice smsDevice2 = (SmsDevice)await SmsDevice.GetDefaultAsync(); 

     // Create a text message - Set destination number and message text 
     SmsTextMessage msg = new SmsTextMessage(); 
     msg.To = "12345678"; 
     msg.Body = "xxxx"; 

     // Send the message asynchronously 
     await smsDevice2.SendMessageAsync(msg); 


    } 

这不会工作(后台服务)

async void DisplayToastAsync(IBackgroundTaskInstance taskInstance, ManualResetEvent manualEventWaiter) 
    { 
     SmsDevice smsDevice2 = (SmsDevice)await SmsDevice.GetDefaultAsync(); 
     // Create a text message - Set destination number and message text 
     SmsTextMessage msg = new SmsTextMessage(); 
     msg.To = "12345678"; 
     msg.Body = "xxx"; 

     // Send the message asynchronously 
     await smsDevice2.SendMessageAsync(msg); 

}

任何人知道为什么?我该如何解决它?

+0

你是什么意思,它不会工作?它有什么作用?它抛出和异常,还是什么? – svick 2012-04-06 18:16:28

+0

没有thrown.i知道这听起来很奇怪,但DisplaytoastAsync中的其他代码得到执行 – CodeGuru 2012-04-07 01:55:56

回答

1

我在做实验,发现你需要填写“From”字段(SmsDevice.AccountPhoneNumber的副本)。尽管如此,我仍然得到了所谓的“通用错误”,但至少它不是“参数错误”。

如果你有兴趣,我的通用错误是0x80548212。

另外 - 我猜你已经这样做了(或者我认为你会得到一个空的短信设备) - 但你需要使应用程序能够在清单中发短信。

更多信息 - 通用错误是我的SIM卡仅针对数据启用。一旦换出包含SMS的SIM卡,代码就可以正常工作。