C# 在创建HttpNotificationChannel时获取异常,但在逐步调试时正常工作
我使用以下代码创建推送通知通道 问题是,当我执行代码时,大多数时候(并非总是),此函数会抛出C# 在创建HttpNotificationChannel时获取异常,但在逐步调试时正常工作,c#,windows-phone-8,push-notification,windows-phone,mpns,C#,Windows Phone 8,Push Notification,Windows Phone,Mpns,我使用以下代码创建推送通知通道 问题是,当我执行代码时,大多数时候(并非总是),此函数会抛出System.NullReferenceException。但是,如果我设置一个断点并一步一步地调试它,它将正常工作并返回有效的HttpNotificationChannel private string AcquirePushChannel() { HttpNotificationChannel currentChannel = HttpNotificationChannel.Find("My
System.NullReferenceException
。但是,如果我设置一个断点并一步一步地调试它,它将正常工作并返回有效的HttpNotificationChannel
private string AcquirePushChannel()
{
HttpNotificationChannel currentChannel = HttpNotificationChannel.Find("MyPushChannel");
if (currentChannel == null)
{
currentChannel = new HttpNotificationChannel("MyPushChannel");
currentChannel.Open();
currentChannel.BindToShellTile();
currentChannel.BindToShellToast();
}
currentChannel.ChannelUriUpdated += (s, e) =>
{
// Code here
};
currentChannel.ShellToastNotificationReceived += async (s, e) =>
{
// Code here
};
return currentChannel.ChannelUri.AbsoluteUri;
}
因为当一步一步地调试时,它工作正常,所以我无法找到问题。有什么想法吗?问题是打开通道是一个异步操作。这就是存在ChannelUriUpdate事件的原因。无法从函数中返回ChannelUri,因为它在函数末尾可能不可用。它将在这个街区内可用
currentChannel.ChannelUriUpdated += (s, e) =>
{
// here the channel uri is available as e.ChannelUri
};
调试时它对您有效的原因是,在您转到最后一行之前触发了事件。似乎这就是问题所在。谢谢