Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在创建HttpNotificationChannel时获取异常,但在逐步调试时正常工作_C#_Windows Phone 8_Push Notification_Windows Phone_Mpns - Fatal编程技术网

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
};

调试时它对您有效的原因是,在您转到最后一行之前触发了事件。

似乎这就是问题所在。谢谢