我正試圖在Windows Phone上實現基於推送通知的應用程序。我已經能夠在模擬器上檢索Channel Uri,並通過我的服務器向模擬器發送Push通知。推送頻道Uri在設備上顯示「無法評估表達式」。在模擬器上完美工作
另一方面,我正面臨着在我的設備上部署相同解決方案的問題。 Uri的用法返回一個NullReferenceException。 Channel Uri顯示「無法評估表達」。
這是我的代碼放在頁面構造函數中。 我試着改變_pushChannelName。
private static string _pushChannelName = "TestApp";
// Constructor
public MainPage()
{
HttpNotificationChannel pushChannel;
InitializeComponent();
pushChannel = HttpNotificationChannel.Find(_pushChannelName);
if (pushChannel == null)
{
MessageBox.Show("NULL");
pushChannel = new HttpNotificationChannel(_pushChannelName);
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(pushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(pushChannel_ErrorOccurred);
pushChannel.ShellToastNotificationReceived +=new EventHandler<NotificationEventArgs>(pushChannel_ShellToastNotificationReceived);
pushChannel.Open();
pushChannel.BindToShellToast();
}
else
{
pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(pushChannel_ChannelUriUpdated);
pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(pushChannel_ErrorOccurred);
pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(pushChannel_ShellToastNotificationReceived);
//System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
textBox1.Text = pushChannel.ChannelUri.ToString();
}
MessageBox.Show(String.Format("Channel Uri is {0}", pushChannel.ChannelUri.ToString()));
}
我也嘗試檢查ChangeUri事件的Uri。該事件不會在設備上觸發,而推送應用工作正常。即使頻道限制不符合。
private void pushChannel_ChannelUriUpdated(Object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(String.Format("Channel Uri is {0}",
e.ChannelUri.ToString()));
});
}
這是否僅在連接/調試器下發生,還是總是發生? – 2012-07-16 12:27:47
如果不在調試器下,應用程序就會崩潰。 – 2012-07-17 06:53:49
即使您只嘗試讀取回調中的URI? – 2012-07-17 12:52:44