Telerik blogs

In the end of 2011 we enhanced our suite with LiveTileHelper. This component can be very useful when creating Secondary tiles. It extends the StandardTileData with two additional properties: VisualElement and BackVisualElement.

Since then we have received a lot of positive feedback about the LiveTileHelper and one of the frequently asked questions was "How to update the tile periodically when some of the data changes?". It is a good question and since it describes a very common scenario we decided to show you how to do it.

To update the tile periodically, we are going to use a PeriodicTask. It inherits from ScheduledTask and ScheduledAction and allows applications to perform processing even when the main application is not in the foreground. If you are not familiar with such Background Agents, I recommend that you read the article "How to: Implement Background Agents for Windows Phone". In short, you need to add a Scheduled Task project to your solution and reference it in your foreground application project. This will create a project, which has a ScheduledAgent class and OnInvoke method inside. This is the method that will be executed every time when the Background Agent is invoked. Now inside this method you can place the logic that will update the tile. You have to use a Dispatcher, because otherwise it would cause an Invalid-cross-thread-reference, because you are trying to update the UI from another thread. You need to add a reference to the Telerik.Windows.BackgroundAgentTools assembly in order to use the LiveTileHelper from inside a BackgroundAgent. Please note that this assembly should be included only in the project that contains the background agent and the Telerik.Windows.Core assembly should be added to the foreground application.

 
protected override void OnInvoke(ScheduledTask task)
{
    Deployment.Current.Dispatcher.BeginInvoke(
        () =>
        {
            RadExtendedTileData extendedData = new RadExtendedTileData();
            extendedData.Title = DateTime.Now.ToString();
            extendedData.Count = 1;
            extendedData.VisualElement = new Rectangle()
            {
                Width = 100,
                Height = 100,
                Fill = new SolidColorBrush(GetRandomColor())
            };
 
            foreach (ShellTile tile in ShellTile.ActiveTiles)
            {
                string uri = tile.NavigationUri.ToString();
                // this will be true only for the secondary tiles that we created and not the application's main tile
                if (uri != "/")
                {
                    LiveTileHelper.UpdateTile(tile, extendedData);
                    break;
                }
            }
        });
 
    NotifyComplete();
}

 

Now, let's go back to the foreground application. We need to add the agent here and we also have to create the tile that will be updated later.

First, let's create the tile by using dynamic visual element from our application:

 

private void CreateLiveTile(object sender, RoutedEventArgs e)
{
    LiveTileHelper.CreateOrUpdateTile(
        new RadExtendedTileData()
        {
            Title = "InitialTile",
            Count = 1,
            VisualElement = this.star
        }, new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}

 

And then add the agent that is going to run in the background:

 

PeriodicTask periodicTask;
string periodicTaskName = "PeriodicAgent";
 
private void AddAgent(string name)
{
    periodicTask = ScheduledActionService.Find(name) as PeriodicTask;
    if (periodicTask != null)
    {
        RemoveAgent(name);
    }
 
    periodicTask = new PeriodicTask(name);
    periodicTask.Description = "LiveTileHelperUpdateTask";
 
    try
    {
        ScheduledActionService.Add(periodicTask);
        ScheduledAgentStackPanel.DataContext = periodicTask;
 
// If debugging is enabled, use LaunchForTest to launch the agent in 5 seconds.
#if(DEBUG_AGENT)
ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(5));
#endif
 
    }
    catch (InvalidOperationException)
    {
        ScheduledAgentCheckBox.IsChecked = false;
    }
    catch (SchedulerServiceException)
    {
        ScheduledAgentCheckBox.IsChecked = false;
    }
}

 

We use the LaunchForTest method to force the invocation of the agent for debugging purposes.

And that's all! When you start the agent, it will make sure to update the tile periodically and when you have some new information, for example from an rss feed, it will be reflected on the tile.

You can download the attached project with full source code here.

If you still don't have RadControls for WindowsPhone, you can download the free trial.

Keep using the Telerik's LiveTileHelper and share your comments and feedback in our forums.


TodorPhoto
About the Author

Todor Petrov

Todor Petrov has been with Telerik since 2011, working on the Android and Windows Phone UI suites. He is passionate about technologies, movies and music.

 



Comments

Comments are disabled in preview mode.