0

我正在做 windows phone 上的推送通知教程。一开始推送通知没有问题。

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

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()都会收到此错误。为什么?

4

2 回答 2

0

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

查看:

CurrentChannel.ConnectionStatus == ChannelConnectionStatus.Connected

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

于 2013-08-05T17:45:19.833 回答
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 事件。

于 2013-08-09T07:45:36.437 回答