【问题标题】:Navigating from App.xaml.cs when receiving push notification接收推送通知时从 App.xaml.cs 导航
【发布时间】:2015-07-16 23:56:45
【问题描述】:

您好,我希望我的应用在收到推送通知时导航到某个页面。我的代码如下:-

ParsePush.ToastNotificationReceived += OnPushNotification;

这是处理push的事件

private async void OnPushNotification(object sender, Windows.Networking.PushNotifications.PushNotificationReceivedEventArgs e)
    {
        var AdFrame = Window.Current.Content as Frame;
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            if (localSettings.Values.ContainsKey("Suspended"))
            {
                String value = localSettings.Values["Suspended"].ToString();
                if (value != null)
                {
                    if (value == "false")
                    {
                        AdFrame.Navigate(typeof(Ad));
                    }
                }
            }
        }

我在var AdFrame = Window.Current.Content as Frame; 收到空引用错误

我已在 App.xaml.cs 中添加了此代码。我只想从当前页面导航到广告页面,无论页面可能处于活动状态。我对 windows 很陌生,任何帮助将不胜感激。

【问题讨论】:

    标签: c# windows-phone-8.1


    【解决方案1】:

    您需要改为显示 Toast 通知:

    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
        ToastTemplateType.ToastImageAndText02);
    
    XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
    stringElements.Item(0).AppendChild(toastXml.CreateTextNode("Hello world!"));
    
    IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
    
    XmlAttribute launchAttribute = toastXml.CreateAttribute("launch");
    launchAttribute.Value = "pass data here"; // TODO: pass data here
    toastNode.Attributes.SetNamedItem(launchAttribute);
    
    ToastNotification toast = new ToastNotification(toastXml);
    ToastNotificationManager.CreateToastNotifier().Show(toast);
    

    当用户点击 toast 通知时,您可以处理 App 启动事件,然后读取数据。

    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        // ...
    
        MainPage page = rootFrame.Content as MainPage;
        page.ParseData(e.Arguments);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 1970-01-01
      • 2019-04-23
      • 2017-05-13
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      相关资源
      最近更新 更多