2

害怕问一个以前可能被问过的问题,但我的搜索技能却无法找到。好的,就到这里吧。

我有 Windows Phone 8 应用程序,当我的应用程序未在前台运行时,我可以在其中接收 TileUpdates 和通知。这是我按照http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202940(v=vs.105).aspx完成的

在那个链接中,我了解到为了在应用程序运行时获取通知,我应该简单地为接收案例附加一个事件。这是我在 AcquirePushChannel() 函数中所做的,如下所示:

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


        if (CurrentChannel == null)
        {
            CurrentChannel = new HttpNotificationChannel("MyPushChannel");
            CurrentChannel.Open();
            if (!CurrentChannel.IsShellToastBound)
            {
                CurrentChannel.BindToShellTile();
            }
            CurrentChannel.BindToShellToast();
            CurrentChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(Push_NotificationRecieved);

        }
        if (!CurrentChannel.IsShellTileBound)
        {
            CurrentChannel.BindToShellToast();
            CurrentChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(Push_NotificationRecieved);
        }

            CurrentChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Push_NotificationChannelChanged);
    }

我已经实现了 CurrentChannel.ChannelUriUpdated,在 channelUri 发生变化的情况下,我执行了一些代码来更改我在云中的 ChannelsTable。我的 Push_NotificationRecieved 看起来像:

private static void Push_NotificationRecieved(object sender, NotificationEventArgs e)
    {
        StringBuilder message = new StringBuilder();
        string relativeUri = string.Empty;

        message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());

        // Parse out the information that was part of the message.
        foreach (string key in e.Collection.Keys)
        {
            message.AppendFormat("{0}: {1}\n", key, e.Collection[key]);

            if (string.Compare(
                key,
                "wp:Param",
                System.Globalization.CultureInfo.InvariantCulture,
                System.Globalization.CompareOptions.IgnoreCase) == 0)
            {
                relativeUri = e.Collection[key];
            }
        }

        // Display a dialog of all the fields in the toast.
        MessageBox.Show(message.ToString());
        //Dispatcher.BeginInvoke((message) => MessageBox.Show(message.ToString()));
    }

我看不出为什么通知没有注册。因为在我的云登录中,我收到了 Toast 通知?

有任何想法吗?此外,据我所知,我可以从代码或类似的东西中显示吐司吗?

额外的

已尝试将功能更改为公共,但对问题没有帮助。任何人都知道为什么事件没有触发。

4

2 回答 2

1

当然,就在我设置赏金运行之后,我让它工作了。所以这里是更新的代码。

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


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

    }
    if (!CurrentChannel.IsShellTileBound)
    {
        CurrentChannel.BindToShellToast();
    }

        CurrentChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Push_NotificationChannelChanged);
        CurrentChannel.ShellToastNotificationReceived += CurrentChannel_ShellToastNotificationReceived;
}

好的,原因是您需要在每次启动时设置事件。然后你会得到想要的属性。然后你必须创建自己的代码来获得你想要的东西:)

于 2013-10-19T10:29:16.453 回答
1

您发布的答案几乎是正确的。从以前你有:

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


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

    }
    if (!CurrentChannel.IsShellTileBound)
    {
        CurrentChannel.BindToShellToast();
    }

        CurrentChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Push_NotificationChannelChanged);
        CurrentChannel.ShellToastNotificationReceived += CurrentChannel_ShellToastNotificationReceived;
}

为此,您必须添加:

private static void CurrentChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
{
    StringBuilder message = new StringBuilder();
    string relativeUri = string.Empty;

    message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());

    // Parse out the information that was part of the message.
    foreach (string key in e.Collection.Keys)
    {
        message.AppendFormat("{0}: {1}\n", key, e.Collection[key]);

        if (string.Compare(
            key,
            "wp:Param",
            System.Globalization.CultureInfo.InvariantCulture,
            System.Globalization.CompareOptions.IgnoreCase) == 0)
        {
            relativeUri = e.Collection[key];
        }
    }

    // Display a dialog of all the fields in the toast.
    MessageBox.Show(message.ToString());
}

因此,您发送的所有内容都在 e.collection 中。所以你可以从服务器发送各种参数。

于 2013-10-19T13:35:49.260 回答