locked
Trying to get rss feed on live tiles RRS feed

  • Question

  • I found this code on a website and I am trying to get it to work but I can't figure it out.

    Here is what it says..

    "

    I’ve covered in a previous post how to create wide tiles for your Windows Phone 7 application. Now it’s time to bring them to life. The easiest way to do so is by using a ScheduledTaskAgent. To create one go ahead and add a new project to your solution and from the project templates list choose ‘Windows Phone Scheduled Task Agent’.

    This will create for you a class called ‘ScheduledAgent’ which inherits ‘ScheduledTaskAgent’. You can leave the constructor of the class and the exception handler as they are. The method that interests us is ‘OnInvoke’. This is the method that will be called for us periodically for as long as the agent is running.

    This is the method in which we will update our live tile. Let’s say we update our tile from a RSS feed. So the first thing we need to do is download our feed."

    protected override void OnInvoke(ScheduledTask task)
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += (s, e) =>
                {
                    DownloadStringCompleted(s, e);
                    NotifyComplete();
                };
            client.DownloadStringAsync(new Uri("http://blogs.windows.com/windows_phone/b/windowsphone/rss.aspx"));
        }

    "Once downloaded, deserialize it, take the first (latest) item and use the title and image to update your live tile."

    private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            StringReader stringReader = new StringReader(e.Result);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
     
            var latestArticle = feed.Items.FirstOrDefault();
            var tile = ShellTile.ActiveTiles.FirstOrDefault();
            if (tile != null)
            {
                var tileData = new FlipTileData();
                tileData.Title = "Windows Phone Blog";
     
                var content = latestArticle.Title.Text + " - " + latestArticle.Summary.Text;
                var image = latestArticle.Links.FirstOrDefault(l => l.MediaType != null && l.MediaType.Contains("image")).Uri;
     
                tileData.BackContent = content;
                tileData.BackgroundImage = image;
     
                tileData.WideBackContent = content;
                tileData.WideBackgroundImage = image;
                tile.Update(tileData);
            }
        }
    }

    "

    Pay attention to where I call NotifyComplete()! This method releases all the resources used by the agent. It is important to call this method because otherwise the task will never end and thus will never start again. It is also very important to call it when everything is done – in this particular case in the DownloadStringCompleted event, after updating the live tiles.

    This leaves us with only one thing left to do: registering the scheduled task. We will do this in the Application_Launching event (found in App.xaml.cs)."

    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        var taskName = "WindowsPhoneBlogSTA";
        PeriodicTask periodicTask = ScheduledActionService.Find(taskName) as PeriodicTask;
        if (periodicTask != null)
            ScheduledActionService.Remove(taskName);
     
        periodicTask = new PeriodicTask(taskName) { Description = "Periodic task to update the tile of <your app>." };
        try
        {
            ScheduledActionService.Add(periodicTask);
            #if DEBUG
                ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(30));
            #endif
        }
        catch (InvalidOperationException) { }
    }

    Ok So I have add the ScheduledTaskAgent to my app. Then I added the first and second set of code to the .cs 

    Than I added the 3rd part of the code to App.xaml.cs like it says to do..

    but now I am stuck where do I go from here to make it work on the tile.. 

    Thanks for your help


    Monday, October 28, 2013 10:11 PM

All replies

  • So what happens when it runs?

    http://pauliom.wordpress.com

    Monday, October 28, 2013 10:26 PM
  • it all runs fine with no errors, but how do I get the info to show up on the tile?
    Do I need to have the info on a secondary tile.

    If so I am at a lose with that and could use some help with that

    • Edited by bhast2 Monday, October 28, 2013 10:33 PM
    Monday, October 28, 2013 10:30 PM
  • Here is my App.xaml.cs

    // ------------------------------------------------------------------------
    // ========================================================================
    // THIS CODE AND INFORMATION ARE GENERATED BY AUTOMATIC CODE GENERATOR
    // ========================================================================
    // Template:   AppCS.tt
    using System;
    using System.CodeDom.Compiler;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Scheduler;
    using Microsoft.Phone.Shell;
    using Services = WPAppStudio.Services;
    using ViewModels = WPAppStudio.ViewModel;
    
    namespace WPAppStudio
    {
        /// <summary>
        /// Application management.
        /// </summary>
        [CompilerGenerated]
        [GeneratedCode("Radarc", "4.0")]
        public partial class App
        {
            /// <summary>
            /// Application execution entry point.
            /// </summary>
            public App()
            {
                // Global handler for uncaught exceptions. 
                UnhandledException += Application_UnhandledException;
    
                // Standard Silverlight initialization
                InitializeComponent();
    
                InitializePhoneApplication();
            }
    
            /// <summary>
            /// Gets/Sets the phone application UI root frame.
            /// </summary>
            public static PhoneApplicationFrame RootFrame { get; private set; }
    
            private void Application_Launching(object sender, LaunchingEventArgs e)
            {
                var taskName = "WindowsPhoneBlogSTA";
        PeriodicTask periodicTask = ScheduledActionService.Find(taskName) as PeriodicTask;
        if (periodicTask != null)
            ScheduledActionService.Remove(taskName);
     
        periodicTask = new PeriodicTask(taskName) { Description = "Periodic task to update the tile of <your app>." };
        try
        {
            ScheduledActionService.Add(periodicTask);
            #if DEBUG
                ScheduledActionService.LaunchForTest(taskName, TimeSpan.FromSeconds(30));
            #endif
        }
        catch (InvalidOperationException) { }
    
            }
    
            private void Application_Activated(object sender, ActivatedEventArgs e)
            {
            }
    
            private void Application_Deactivated(object sender, DeactivatedEventArgs e)
            {
            }
    
            private void Application_Closing(object sender, ClosingEventArgs e)
            {
            }
    
            private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
            {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
            }
    
            private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            {
                if (Debugger.IsAttached)
                {
                    Debugger.Break();
                }
            }
    
            private bool _phoneApplicationInitialized;
    
            private void InitializePhoneApplication()
            {
                if (_phoneApplicationInitialized)
                    return;
    
                RootFrame = new TransitionFrame();
                RootFrame.Navigated += CompleteInitializePhoneApplication;
    
                RootFrame.NavigationFailed += RootFrame_NavigationFailed;
    
                _phoneApplicationInitialized = true;
            }
    
    
            private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
            {
                if (RootVisual != RootFrame)
                    RootVisual = RootFrame;
    
                RootFrame.Navigated -= CompleteInitializePhoneApplication;
            }
        }
    }
    

    Here is my ScheduledAgent.cs

    using System.Diagnostics;
    using System.Windows;
    using Microsoft.Phone.Scheduler;
    using System.Net;
    using System.IO;
    using System;
    using System.ServiceModel.Syndication;
    using System.Xml;
    using Microsoft.Phone.Shell;
    using System.Linq;
    namespace ScheduledTaskAgent1
    {
        public class ScheduledAgent : ScheduledTaskAgent
        {
            /// <remarks>
            /// ScheduledAgent constructor, initializes the UnhandledException handler
            /// </remarks>
            static ScheduledAgent()
            {
                // Subscribe to the managed exception handler
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    Application.Current.UnhandledException += UnhandledException;
                });
            }
    
            /// Code to execute on Unhandled Exceptions
            private static void UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            {
                if (Debugger.IsAttached)
                {
                    // An unhandled exception has occurred; break into the debugger
                    Debugger.Break();
                }
            }
    
            /// <summary>
            /// Agent that runs a scheduled task
            /// </summary>
            /// <param name="task">
            /// The invoked task
            /// </param>
            /// <remarks>
            /// This method is called when a periodic or resource intensive task is invoked
            /// </remarks>
            protected override void OnInvoke(ScheduledTask task)
            {
                //TODO: Add code to perform your task in background
    
               WebClient client = new WebClient();
            client.DownloadStringCompleted += (s, e) =>
                {
                    DownloadStringCompleted(s, e);
                    NotifyComplete();
                };
            client.DownloadStringAsync(new Uri("http://blogs.windows.com/windows_phone/b/windowsphone/rss.aspx"));
        
            }
            private void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    StringReader stringReader = new StringReader(e.Result);
                    XmlReader xmlReader = XmlReader.Create(stringReader);
                    SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
    
                    var latestArticle = feed.Items.FirstOrDefault();
                    var tile = ShellTile.ActiveTiles.FirstOrDefault();
                    if (tile != null)
                    {
                        var tileData = new FlipTileData();
                        tileData.Title = "Windows Phone Blog";
    
                        var content = latestArticle.Title.Text + " - " + latestArticle.Summary.Text;
                        var image = latestArticle.Links.FirstOrDefault(l => l.MediaType != null && l.MediaType.Contains("image")).Uri;
    
                        tileData.BackContent = content;
                        tileData.BackgroundImage = image;
    
                        tileData.WideBackContent = content;
                        tileData.WideBackgroundImage = image;
                        tile.Update(tileData);
                    }
                }
            }
        
        }
    }

    But don't know what to do from here

    Monday, October 28, 2013 10:45 PM
  • You code is updating the primary tile. As long as that is pinned and the values you are assigning are present, then it should update. Try just hardcoding some well known values in there first.

    http://pauliom.wordpress.com

    Tuesday, October 29, 2013 10:07 AM
  • i also tried this code but having the same issue in updating the tile with an rss feed.....................

    btw bhast2, did you create a seperate class for SydnciationFeed?


    Thanks

    Monday, January 20, 2014 8:57 AM
  • I don't believe you can put NotifyComplete in the oncomplete, the task will complete before that is called

    http://pauliom.wordpress.com

    Monday, January 20, 2014 2:26 PM