从 Windows Phone 8.1 操作中心选择通知

Selecting a Notification from the Windows Phone 8.1 Action Center

我正在像这样向设备发送 Toast 通知。

private void SendNotification(string text, string activatedargs = null) {
    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

    XmlNodeList elements = toastXml.GetElementsByTagName("text");
    foreach(IXmlNode node in elements) {
        node.InnerText = text;
    }

    if(!string.IsNullOrEmpty(activatedargs)) {
        ((XmlElement)toastXml.SelectSingleNode("/toast")).SetAttribute("launch", activatedargs);
    }

    ToastNotification notification = new ToastNotification(toastXml);
    notification.Activated += notification_Activated;
    ToastNotificationManager.CreateToastNotifier().Show(notification);
}

如您所见,我正在为 Activated 事件添加一个回调函数。当 toast 通知弹出窗口出现在屏幕顶部时,这非常有用。但是当通知消失并且通知仅在操作中心可见时,选择通知时永远不会调用回调。该应用程序可以正常打开,但无法打开包含基于所选通知的信息的视图。

因此,如果我发送了三个 toast 通知,"A"、"B" 和 "C"。这三个通知列在我的应用程序名称下方的操作中心中。具体来说,当点击通知 "B" 时,如何将应用程序打开到与 "B" 相关的特定视图?

您应该将 activatedargs 设置为包括身份识别 "A"、"B" 或 "C"。

activatedargs = "B"

然后在 App.xaml.cs 中您可以获得标识并基于该标识导航到正确的视图

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    string launchString = launchEventArgs.Arguments;
    if (!String.IsNullOrEmpty(launchString))
    {
        // Get the identification and navigate to correct view
        switch(launchString)
        {
            case "B": NavigateToViewModel<BViewModel>();
        }
    }
}