Azure移动服务频道中的空例外

时间:2013-08-03 16:59:40

标签: azure push-notification azure-mobile-services mpns windows-phone-8

我在Windows手机上做推送通知教程。一开始,推送通知没有问题。

然而,几天后我打开了编码并得到了这个错误:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=UtemFtmkDB
  StackTrace:
       at UtemFtmkDB.MainPage.ButtonSave_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.<OnMouseLeftButtonUp>b__3()
  InnerException: 

在App.xaml.cs中:

private void AcquirePushChannel()
{
    CurrentChannel = HttpNotificationChannel.Find("MyPushChannel");


    if (CurrentChannel == null)
    {
        CurrentChannel = new HttpNotificationChannel("MyPushChannel");
        CurrentChannel.Open();
        CurrentChannel.BindToShellTile();
    }


}

每当我从:App.CurrentChannel.ChannelUri.ToString()检索数据时,都会收到此错误消息。为什么呢?

2 个答案:

答案 0 :(得分:0)

频道可能尚未达到“已连接”状态。

检查:

CurrentChannel.ConnectionStatus == ChannelConnectionStatus.Connected

一旦这是真的,你应该为ChannelUri提供一个非空值。

答案 1 :(得分:0)

您想要与 ChannelUriUpdated 事件挂钩:

private void AcquirePushChannel()
{
    CurrentChannel = HttpNotificationChannel.Find("MyPushChannel");

    if(null != CurrentChannel)
    {
        CurrentChannel.ChannelUriUpdated += CurrentChannelOnChannelUriUpdated;        
    }
    else
    {
        CurrentChannel = new HttpNotificationChannel("MyPushChannel");
        CurrentChannel.ChannelUriUpdated += CurrentChannelOnChannelUriUpdated;        

        CurrentChannel.Open();
        CurrentChannel.BindToShellTile();
    }
}

private void CurrentChannelOnChannelUriUpdated(object sender, NotificationChannelUriEventArgs args)
{
    // you can now get the URI from args.ChannelUri
}

请注意,现有频道的频道uri也可能会发生变化。因此,无论您是否创建新频道,都应该收听ChannelUriUpdated事件。