Windows runtime 当应用程序在Windows Phone 8.1运行时应用程序上运行时,如何使用Toast通知?

Windows runtime 当应用程序在Windows Phone 8.1运行时应用程序上运行时,如何使用Toast通知?,windows-runtime,windows-phone-8.1,toast,Windows Runtime,Windows Phone 8.1,Toast,我正在开发一个预算管理应用程序。我的应用程序记录最终用户输入的费用和收入。但我想在应用程序中添加一项功能:用户可以添加定期发生的收入/支出,例如每10天一次 我认为ScheduledToastNotification是这个问题的解决方案,因为我希望用户批准创建此事务,以完成流程。但问题是,当应用程序运行时,即使在屏幕上显示ToastNotification时从后台运行,点击它也不会像预期的那样工作:启动参数不会传递给我的应用程序。我发现这样做的原因是,由于应用程序正在运行,因此不会触发OnLau

我正在开发一个预算管理应用程序。我的应用程序记录最终用户输入的费用和收入。但我想在应用程序中添加一项功能:用户可以添加定期发生的收入/支出,例如每10天一次

我认为ScheduledToastNotification是这个问题的解决方案,因为我希望用户批准创建此事务,以完成流程。但问题是,当应用程序运行时,即使在屏幕上显示ToastNotification时从后台运行,点击它也不会像预期的那样工作:启动参数不会传递给我的应用程序。我发现这样做的原因是,由于应用程序正在运行,因此不会触发OnLaunched事件

另一个问题是,我无法确保一切正常,因为我无法调试此类行为:ToastNotification仅在应用程序未运行时相关,但我无法调试未运行的应用程序。那么,当我通过ToastNotification启动我的应用程序时,我如何确切地知道会发生什么呢

有什么解决方案可以帮助我实现这个目标吗?下面是我的代码:

public static void CreateNotification(Class.Action Transaction)
    {
        var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
        var ToastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
        var Strings = ToastXml.GetElementsByTagName("text");
        Strings[0].AppendChild(ToastXml.CreateTextNode(loader.GetString("NotificationText")));
        Strings[1].AppendChild(ToastXml.CreateTextNode(Transaction.Title + " : " + Transaction.SAmount));

        IXmlNode toastNode = ToastXml.SelectSingleNode("/toast");
        XmlElement audio = ToastXml.CreateElement("audio");

        audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
        toastNode.AppendChild(audio);

        ((XmlElement)toastNode).SetAttribute("launch", Transaction.Id.ToString());

        var Toast = new ScheduledToastNotification(ToastXml, Transaction.NextOccurence);

        Toast.Id = Transaction.Id + "";

        ToastNotificationManager.CreateToastNotifier().AddToSchedule(Toast);
    }
这就是我打算如何使用参数:

int TransactionID = 0;

        if (int.TryParse((string)e.Parameter, out TransactionID))
        {
            var loader = new Windows.ApplicationModel.Resources.ResourceLoader();

            Class.Action RecurentAction = new Class.Action((from act in ActCRUD.GetActions()
                                                                where act.Id == TransactionID
                                                                select act).FirstOrDefault());

            Flyout flyer = new Flyout();
            StackPanel content = new StackPanel();

            TextBlock editMessage = new TextBlock();
            editMessage.FontSize = 20;
            editMessage.Margin = new Thickness(15, 50, 15, 15);
            TextBox newCategoryName = new TextBox();
            newCategoryName.FontSize = 20;
            newCategoryName.Margin = new Thickness(15, 15, 15, 15);
            Button confirmEdit = new Button();
            confirmEdit.Margin = new Thickness(15, 15, 15, 15);

            editMessage.Text = loader.GetString("EnterComment");
            newCategoryName.PlaceholderText = loader.GetString("EnterCommentPlaceholder");
            confirmEdit.Content = loader.GetString("OkText");
            confirmEdit.Tapped += new TappedEventHandler(delegate(object o, TappedRoutedEventArgs i)
            {
                RecurentAction.Comment = newCategoryName.Text;
                ActCRUD.AddAction(RecurentAction);
                MainPage.CreateNotification((from Class.Action action in ActCRUD.GetActions()
                                             orderby action.Id descending
                                             select action).First());

                flyer.Hide();
                newCategoryName.Text = "";
            });

            content.Children.Add(editMessage);
            content.Children.Add(newCategoryName);
            content.Children.Add(confirmEdit);

            flyer.Content = content;

            MessageDialog NotificationConfirmation = new MessageDialog(loader.GetString("ConfirmRecurentAction"));
            NotificationConfirmation.Commands.Add(new UICommand(loader.GetString("Confirm"), (commands) =>
            {
                flyer.ShowAt(PivotFintatanana);
            }));

            NotificationConfirmation.Commands.Add(new UICommand(loader.GetString("DoNotConfirm"), (commands) =>
            {

            }));

            await NotificationConfirmation.ShowAsync();
        }
我要寻找的是一个ScheduledToastNotification,它可以通过强制触发OnLaunched事件或其他解决方法将其参数发送到应用程序,即使这个应用程序正在运行

谢谢,
问候。

如果在应用程序运行时显示toast,并且如果您点击toast,则确实会调用OnLaunched函数。然而,VisualStudio模板提供的OnLaunched中的代码检查rootFrame的内容是否为null,如果不是null,那么如果应用程序正在运行,则只显示正在运行的实例

如果应用程序已在运行,则跳过以下代码:

if (rootFrame.Content == null) //shouldn't be null if app is running
{  
    if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
    {
        throw new Exception("Failed to create initial page");
    }
}
因此,您需要在这部分代码中添加逻辑,比如

if(e.Arguments != null)
{
    //navigate to details page with e.Arguments
}    
else if (rootFrame.Content == null)
{
    if (!rootFrame.Navigate(typeof(BlankPage1), e.Arguments))
    {
        throw new Exception("Failed to create initial page");
    }
}

非常感谢您@Abhishek!这绝对解决了我的问题。仅供记录:这是我在这个网站上的第一个问题。因此,我要加倍感谢: