【发布时间】:2012-07-14 11:59:49
【问题描述】:
我正在尝试在 Windows Phone 上实现基于推送通知的应用程序。我已经能够在模拟器上检索 Channel 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。该事件不会在设备上触发,而 Push 应用程序工作正常。连频道限制都达不到。
private void pushChannel_ChannelUriUpdated(Object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(String.Format("Channel Uri is {0}",
e.ChannelUri.ToString()));
});
}
【问题讨论】:
-
只有在调试器绑定/下时才会发生这种情况,还是总是会发生?
-
如果不在调试器下,应用就会崩溃。
-
即使你只尝试在回调中读取 URI?
-
回调根本没有触发。
-
如果你删除了保证它崩溃的代码,回调将有机会触发。你有没有解决这个问题,并尝试查看回调是否被调用?
标签: windows-phone-7 push-notification nullreferenceexception mpns