在Windows Phone 8.1中生成Chanal Uri时发生'System.NullReferenceException'

时间:2015-10-30 12:29:33

标签: windows-phone-7 windows-phone-8 windows-phone-8.1

每当我尝试在Windows Phone 7和Windows Phone 8中创建推送通知时,我的代码工作正常,但每当我尝试在Windows Phone 8.1上构建相同的代码时,它会在生成Chanal Uri时显示“System.NullReferenceException”在Windows Phone 8.1中。

代码如下:

            String channelUri="XYZ";
            HttpNotificationChannel myPushChannel = HttpNotificationChannel.Find(channelUri);
            if (null == myPushChannel)
            {

                    myPushChannel = new HttpNotificationChannel(channelUri);
                    myPushChannel.Open();
                    myPushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(myPushChannel_ShellToastNotificationReceived);
                    myPushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(myPushChannel_ChannelUriUpdated);
                    myPushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(myPushChannel_ErrorOccurred);
                    myPushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(myPushChannel_HttpNotificationReceived);
                    myPushChannel.BindToShellTile();
                    myPushChannel.BindToShellToast();
                    uri = myPushChannel.ChannelUri;
            }
            else
            {

                if (myPushChannel != null)
                {
                    myPushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(myPushChannel_ShellToastNotificationReceived);
                    myPushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(myPushChannel_ChannelUriUpdated);
                    myPushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(myPushChannel_ErrorOccurred);
                    myPushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(myPushChannel_HttpNotificationReceived);
                    uri = myPushChannel.ChannelUri;
                    if (!myPushChannel.IsShellToastBound)
                        myPushChannel.BindToShellToast();
                        if (!myPushChannel.IsShellTileBound)
                        myPushChannel.BindToShellTile();
                }

            }
        }

在Windows Phone 7/8中一切正常,但在Windows Phone 8.1上无法正常工作。

1 个答案:

答案 0 :(得分:0)

如果我们谈论的是WP8.1 Universal Apps,而不是Silverlight,您应该使用下面的行来获取PushNotificationChannel:

PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();